How to Activate a Plugin Automatically When Your Plugin is Activated

One common question I’ve encountered is how to automatically activate another plugin when your plugin is installed. While you shouldn’t activate a plugin without permission, I’ve seen premium plugins do this when they require a free version of the plugin to be installed, and it’s been a positive user experience.

Let’s dive into a sample plugin and demonstrate how to activate a plugin programmatically.

Getting the path of the plugin to activate

First, let’s pick on a plugin to activate. I’m going to choose my free plugin Comment Edit Core just because I already know the innards of it.

My plugin setup is as follows:

.
└── wp-content/plugins/
    ├── dlx-plugin-activation-example/
    │   └── dlx-plugin-activation-example.php
    └── simple-comment-editing/
        └── index.phpCode language: AsciiDoc (asciidoc)

First, you need to know the path of the plugin to activate. The format is the plugin’s folder followed by its main activation file. The main activation file is the one containing the plugin’s headers.

For my free plugin, I know the path is: simple-comment-editing/index.php

Setting up the base plugin

Let’s set up the plugin’s headers and namespace first.

<?php
/**
 * Plugin Name: DLX Plugin Activation Example
 * Plugin URI: https://dlxplugins.com
 * Description: Example plugin for the DLX Plugin Activation plugin.
 * Version: 1.0.0
 * Author: DLX Plugins
 * Author URI: https://dlxplugins.com/
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: dlx-plugin-activation-example
 * Domain Path: /languages
 * Requires at least: 5.0
 * Tested up to: 6.4
 * Requires PHP: 7.4
 * Network: false
 *
 * @package DLXPluginActivationExample
 * @since 1.0.0
 */

namespace DLXPlugins\PluginActivationExample;

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}Code language: PHP (php)

I’m going to introduce one helper function, which will tell us if a plugin is active for a site or Multisite network.


namespace DLXPlugins\PluginActivationExample;

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

function is_plugin_active( $plugin_path = '' ) {
	if ( empty( $plugin_path ) ) {
		return false;
	}
	if ( is_multisite() ) {
		if ( is_plugin_active_for_network( $plugin_path ) ) {
			return true;
		}
	}
	return is_plugin_active( $plugin_path );
}Code language: PHP (php)

The is_plugin_active is a local function that returns whether a plugin is active or not. Feel free to repurpose it.

Setting up the activation hook and activating the plugin

register_activation_hook function
register_activation_hook function

Using function register_activation_hook, we can detect when our plugin is activated, and then activate the “free” version.

register_activation_hook( __FILE__, __NAMESPACE__ . '\plugin_activate' );

function plugin_activate() {
	$plugin_to_activate = 'simple-comment-editing/index.php';
	if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_to_activate ) ) {
		if ( ! is_plugin_active( $plugin_to_activate ) ) {
			activate_plugin( $plugin_to_activate, '', false, false );
		}
	}
}Code language: PHP (php)

I pass in my “path”, which is simple-comment-editing/index.php. I then use WordPress function activate_plugin to activate the plugin if it’s not active and the file exists.

activate_plugin function
activate_plugin function

WordPress function activate_plugin takes in the following arguments:

  • $path – This is the relative path to the plugin’s main initialization file.
  • $redirect – Where to redirect the user after activation. Leave blank for no redirect.
  • $network_wide – Whether to network activate the plugin.
  • $silent – Whether to show any display messages or not.

Network activating a plugin programmatically

For the network_wide argument, in the activate_plugin function, we can get a little creative. We can modify the function so that if our plugin is network-activated, the other one should be too.

register_activation_hook( __FILE__, __NAMESPACE__ . '\plugin_activate' );

function plugin_activate() {
	$plugin_to_activate = 'simple-comment-editing/index.php';
	$network_enable     = false;
	if ( is_multisite() ) {
		if ( is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
			$network_enable = true;
		}
	}
	if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_to_activate ) ) {
		if ( ! is_plugin_active( $plugin_to_activate ) ) {
			activate_plugin( $plugin_to_activate, '', $network_enable, false );
		}
	}
}Code language: PHP (php)

This snippet assumes register_activation_hook is taking place in our base plugin’s initialization file. It checks if the installation is Multisite. If it is, then it does a check to see if the plugin is Network Active. If so, then the plugin to be activated is network-activated.

Conclusion

In this brief tutorial, I demonstrate how to activate a plugin when yours is activated programmatically.

Here is the complete code for this tutorial:

<?php
/**
 * Plugin Name: DLX Plugin Activation Example
 * Plugin URI: https://dlxplugins.com
 * Description: Example plugin for the DLX Plugin Activation plugin.
 * Version: 1.0.0
 * Author: DLX Plugins
 * Author URI: https://dlxplugins.com/
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: dlx-plugin-activation-example
 * Domain Path: /languages
 * Requires at least: 5.0
 * Tested up to: 6.4
 * Requires PHP: 7.4
 * Network: false
 *
 * @package DLXPluginActivationExample
 * @since 1.0.0
 */

namespace DLXPlugins\PluginActivationExample;

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

function is_plugin_active( $plugin_path = '' ) {
	if ( empty( $plugin_path ) ) {
		return false;
	}
	if ( is_multisite() ) {
		if ( is_plugin_active_for_network( $plugin_path ) ) {
			return true;
		}
	}
	return is_plugin_active( $plugin_path );
}

register_activation_hook( __FILE__, __NAMESPACE__ . '\plugin_activate' );

function plugin_activate() {
	$plugin_to_activate = 'simple-comment-editing/index.php';
	$network_enable     = false;
	if ( is_multisite() ) {
		if ( is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
			$network_enable = true;
		}
	}
	if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_to_activate ) ) {
		if ( ! is_plugin_active( $plugin_to_activate ) ) {
			activate_plugin( $plugin_to_activate, '', $network_enable, false );
		}
	}
}
Code language: PHP (php)

Like the tutorial you just read? There's more like it.

There's more where that came from. Enter your email and I'll send you more like it.

This field is for validation purposes and should be left unchanged.

Ask a Question or Leave Feedback

Your email address will not be published. Required fields are marked *