Skip to content

Instantly share code, notes, and snippets.

@Basilakis
Forked from mathetos/plugin.php
Created November 26, 2015 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Basilakis/7e62e74e2ba5a7f1eb1e to your computer and use it in GitHub Desktop.
Save Basilakis/7e62e74e2ba5a7f1eb1e to your computer and use it in GitHub Desktop.
Dependent Plugin Activation/Deactivation and Alert
<?php
/*
* Dependent Plugin Activation/Deactivation
*
* Sources:
* 1. https://pippinsplugins.com/checking-dependent-plugin-active/
* 2. http://10up.com/blog/2012/wordpress-plug-in-self-deactivation/
*
*/
function child_plugin_init() {
// If Parent Plugin is NOT active
if ( current_user_can( 'activate_plugins' ) && !class_exists( 'Parent Plugin Class' ) ) {
add_action( 'admin_init', 'my_plugin_deactivate' );
add_action( 'admin_notices', 'my_plugin_admin_notice' );
// Deactivate the Child Plugin
function my_plugin_deactivate() {
deactivate_plugins( plugin_basename( __FILE__ ) );
}
// Throw an Alert to tell the Admin why it didn't activate
function my_plugin_admin_notice() {
echo "<div class=\"error\"><p><strong>" . __( '"The Child Plugin"</strong> requires "The Parent Plugin" to function correctly. Please activate "The Parent Plugin" before activating "The Child Plugin". For now, the plug-in has been <strong>deactivated</strong>.', 'textdomain' ) . "</p></div>";
if ( isset( $_GET['activate'] ) )
unset( $_GET['activate'] );
}
} else {
// include all your plugin files here
}
}
add_action( 'plugins_loaded', 'child_plugin_init' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment