1

Hi I'm wondering if someone can point me in the right direction here. I'm having trouble figuring out how deactivate_plugins() works. I've been using variations of the below for awhile now and it's always seemed to work until recently.

register_activation_hook(__FILE__, 'my_plugin_activate_deactivate');
add_action('after_switch_theme', 'my_plugin_activate_deactivate');

function my_plugin_activate_deactivate() {
    if ( !function_exists ('mythemename') {
      deactivate_plugins( plugin_basename(__FILE__) );
    }
}

The first problem is that the register_activation_hook part doesn't do anything at all... the plugin activates regardless of whether or not mythemename function exists.

I even tried just doing this, but the plugin still activates.

register_activation_hook(__FILE__, 'my_plugin_activate_deactivate');

function my_plugin_activate_deactivate() {
      deactivate_plugins( plugin_basename(__FILE__) );
}

Even worse though using the after_switch_theme part throws a 500 server error, when switching themes and it takes manually disabling the plugin through ftp/sftp.

My goal is to have a "theme functionality plugin" that checks if the theme is active and if not deactivates itself. What am I missing here? Something obvious? I feel like there's a spelling error or something stupid because this has always seemed to work in the past...

Update : I activated an error log on the server and am getting this error about deactivate_plugins():

Call to undefined function deactivate_plugins() in /path/to/plugin/plugin-name.php on line 25
2

You may need to have this line in your code

require_once( ABSPATH . 'wp-admin/includes/plugin.php' );

The plugin.php file is not automatically included.

  • 1
    I've seen this used a couple times, but not really sure why. Do you know why that's necessary? – Bryan Willis Mar 11 '16 at 2:17
  • @BryanWillis The function deactivate_plugins is defined in that file. Hence we use it. – Maria Daniel Deepak Apr 6 '17 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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