Skip to content

Instantly share code, notes, and snippets.

@mathetos
Last active April 13, 2023 16:48
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save mathetos/7161f6a88108aaede32a to your computer and use it in GitHub Desktop.
Save mathetos/7161f6a88108aaede32a 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() {
$dpa_child_plugin = __( 'Child Plugin', 'textdomain' );
$dpa_parent_plugin = __( 'Parent Plugin', 'textdomain' );
echo '<div class="error"><p>'
. sprintf( __( '%1$s requires %2$s to function correctly. Please activate %2$s before activating %1$s. For now, the plugin has been deactivated.', 'textdomain' ), '<strong>' . esc_html( $dpa_child_plugin ) . '</strong>', '<strong>' . esc_html( $dpa_parent_plugin ) . '</strong>' )
. '</p></div>';
if ( isset( $_GET['activate'] ) )
unset( $_GET['activate'] );
}
} else {
// include all your plugin files here
}
}
add_action( 'plugins_loaded', 'child_plugin_init' );
@kevinwhoffman
Copy link

Hi Matt, cool stuff :) Here's a fork I created that improves translation by removing HTML markup from the translated string: https://gist.github.com/kevinwhoffman/5f3e5d613d1effb3cec4#file-plugin-php-L26-L31

Feel free to merge it in if you agree.

@mathetos
Copy link
Author

Thanks @kevinwhoffman Merged! Nice attention to detail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment