Software Developer

Deactivate Plugin If Unchecked in Theme Options

This is a function for a weird case. In the strange case that you are including or packaging a plugin with a theme, and you only allow the plugin to be activated if they mark a checkbox in the Theme Options page, and you don’t want the plugin to ever remain activated if they UNCHECK that preference in theme options.

This function deactivates the plugin if they uncheck the option in the theme options. This allows them to change their mind, selecting and deselecting the plugin, and you don’t have to worry about unwanted plugin functionality to remain if they deselect the option.

function smartestb_deactivate_reviewsplugin() {
	$plugin = 'your-plugin/your-plugin.php';// your plugin file relative to the /plugins/ directory
 
	// if plugin is active and option is unchecked
	if( in_array( $plugin, apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) && ( get_option('YOUR-THEME-OPTION-THAT-ACTIVATED-PLUGIN') !== 'true' ) ) {
		
			deactivate_plugins( $plugin );
			//wp_delete_post(get_option('custom_created_page_id'));// sometimes you want this

	}
}
add_action( 'admin_init', 'smartestb_deactivate_reviewsplugin' );

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]