0

I'm building two mutually incompatible plugins. When either one is activated, the other must be deactivated. I'm using the code below to accomplish this.

It works fine the first time I upload and activate the second plugin, but if I try to activate the inactive plugin in the admin panel, it just deactivates both plugins. (Though I get a confirmation message saying "plugin activated." Liars!) I am then able to activate either one of the two plugins.

Any ideas what the problem is?

function deactivate_other_plugin() {
    if( is_plugin_active('plugin_folder/plugin_file.php') ) {
    deactivate_plugins('plugin_folder/plugin_file.php');    
    }
}
add_action( 'admin_init', array( $this, 'deactivate_other_plugin' ) );
2
  • 3
    Why would you want this? Expected behaviour is that a plugin would detect the other plugin and return early without doing anything except maybe putting an admin notice up. Also &$this is a hack to make things work on PHP4, you should use $this instead when registering functions as is customary in PHP 5 ( unless your plugin is aimed for a server stack from 2003 and WordPress v1.5 )
    – Tom J Nowell
    Feb 17, 2016 at 4:30
  • Ok, so it would be better to just return early and give a notice to deactivate the other plugin before activating. Thanks for the tip about &$this being obsolete.
    – j8d
    Feb 17, 2016 at 4:43

1 Answer 1

0

Ok, based on the comments, I decided to just prevent activation and send an admin notice instead.

add_action( 'admin_notices', 'lsmi_admin_notice' );
add_action( 'network_admin_notices', 'lsmi_admin_notice' );
function lsmi_admin_notice() {
    if( class_exists ( 'Location_Specific_Menu_Items_By_City' ) ){
        global $pagenow;
        if( $pagenow == 'plugins.php' ){
            deactivate_plugins ( 'lsmi-by-country/LSMI_Country.php' );
            if ( current_user_can( 'install_plugins' ) ) {
                echo '<div id="error" class="error notice is-dismissible"><p>Error. Please deactivate LSMI by City first and try again.</div>';
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.