Plugin Deactivate Self on Activation Errors

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

Topic deactivated-plugin deactivation activation plugins themes Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.