Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dianjuar/9a398c9e86a20a30868eee0c653e0ca4 to your computer and use it in GitHub Desktop.
Save dianjuar/9a398c9e86a20a30868eee0c653e0ca4 to your computer and use it in GitHub Desktop.
Make a WordPress plugin dependent of another plugin

How to make WordPress a plugin require another plugin?

Put this function on the main php file of your dependent plugin and call it with the rigth parameters.

/**
 * Verify if a plugin is active, if not deactivate the actual plugin an show an error
 * @param  [string]  $my_plugin_name
 *                   The plugin name trying to activate. The name of this plugin
 *                   Ex:
 *                   WooCommerce new Shipping Method
 *
 * @param  [string]  $dependency_plugin_name
 *                   The dependency plugin name.
 *                   Ex:
 *                   WooCommerce.
 *
 * @param  [string]  $path_to_plugin
 *                   Path of the plugin to verify with the format 'dependency_plugin/dependency_plugin.php'
 *                   Ex:
 *                   woocommerce/woocommerce.php
 *
 * @param  [string] $textdomain
 *                  Text domain to looking the localization (the translated strings)
 *
 * @param  [string] $version_to_check
 *                  Optional, verify certain version of the dependent plugin
 */
function bikecoders_is_this_plugin_active($my_plugin_name, $dependency_plugin_name, $path_to_plugin, $textdomain = '', $version_to_check = null) {

    # Needed to the function "deactivate_plugins" works
    include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

    if( !is_plugin_active( $path_to_plugin ) )
    {
        # Deactivate the current plugin
        deactivate_plugins( plugin_basename( __FILE__ ) );

        # Show an error alert on the admin area
        add_action( 'admin_notices', function() use($my_plugin_name, $dependency_plugin_name, $textdomain)
        {
            ?>
            <div class="updated error">
                <p>
                    <?php
                    echo sprintf(
                        __( 'The plugin <strong>"%s"</strong> needs the plugin <strong>"%s"</strong> active', $textdomain ),
                        $my_plugin_name, $dependency_plugin_name
                    );
                    echo '<br>';
                    echo sprintf(
                        __( '<strong>%s has been deactivated</strong>', $textdomain ),
                        $my_plugin_name
                    );
                    ?>
                </p>
            </div>
            <?php
            if ( isset( $_GET['activate'] ) )
                unset( $_GET['activate'] );
        } );
    }
    else {

        # If version to check is not defined do nothing
        if($version_to_check === null)
            return;

        # Get the plugin dependency info
        $depPlugin_data = get_plugin_data( WP_PLUGIN_DIR.'/'.$path_to_plugin);

        # Compare version
        $error = !version_compare ( $depPlugin_data['Version'], $version_to_check, '>=') ? true : false;

        if($error) {

            # Deactivate the current plugin
            deactivate_plugins( plugin_basename( __FILE__ ) );

            add_action( 'admin_notices', function() use($my_plugin_name, $dependency_plugin_name, $version_to_check, $textdomain)
            {
                ?>
                <div class="updated error">
                    <p>
                        <?php
                        echo sprintf(
                            __( 'The plugin <strong>"%s"</strong> needs the <strong>version %s</strong> or newer of <strong>"%s"</strong>', $textdomain ),
                            $my_plugin_name,
                            $version_to_check,
                            $dependency_plugin_name
                        );
                        echo '<br>';
                        echo sprintf(
                            __( '<strong>%s has been deactivated</strong>', $textdomain ),
                            $my_plugin_name
                        );
                        ?>
                    </p>
                </div>
                <?php
                if ( isset( $_GET['activate'] ) )
                    unset( $_GET['activate'] );
            } );
        }
    }# else
}

Be aware, the parameter of the function deactivate_plugins needs to be with out the init /.

Example:

WRONG -> deactivate_plugins('/my_plugin/my_plugin.php');

THE RIGHT WAY : deactivate_plugins('my_plugin/my_plugin.php');

@kwatts22
Copy link

I had to make some adjustment to get it to work.

on this
function bikecoders_is_this_plugin_active($my_plugin_name, $dependency_plugin_name, $path_to_plugin, $textdomain = '', $version_to_check = null)

I had to change to this
public function whmcs_verbinder_is_this_plugin_active($my_plugin_name = 'WHMCS Verbinder' , $dependency_plugin_name = 'WooCommerce', $path_to_plugin = 'woocommerce/woocommerce.php', $textdomain = 'whmcs-verbinder', $version_to_check = null, $my_plugin_path = 'whmcs-verbinder/whmcs-verbinder.php')

notice how I added the $my_plugin_path it just referees to your folder-name/folder-name.php

Then the I had to change
deactivate_plugins( plugin_basename( __FILE__ ) );

to this because the plugin wouldn't deactivate
deactivate_plugins( $my_plugin_path );

I also had to just write the error messages out because it wouldn't show anything with the %s. I think that is because these variables were not set but didn't investigate the issue.
add_action( 'admin_notices', function() use($my_plugin_name, $dependency_plugin_name, $version_to_check, $textdomain) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment