The current trend in plugin development is all about having a core plugin and then developing other addons. But how do you prevent an addon plugin being activated when the core (parent) plugin is not activated?
The way to do that is to check if a particular class, function, or constant from the core plugin exists:
[php]
// Code from
// Check to see if your plugin has already been loaded. This can be done in several
// ways – here are a few examples:
//
// Check for a class:
// if (!class_exists(‘MyPluginClass’)) {
//
// Check for a function:
// if (!function_exists(‘my_plugin_function_name’)) {
//
// Check for a constant:
// if (!defined(‘MY_PLUGIN_CONSTANT’)) {
[/php]
Video SEO for WordPress SEO
Here’s how Yoast does it for his Video SEO for WordPress SEO plugin. The core plugin has a constant WPSEO_VERSION, which stores the current version number of the plugin. The addon plugin then checks for the presence of that constant before it can be used. An elegant solution indeed.
[php]
/**
* Initialize the Video SEO module on plugins loaded, so WP SEO should have set its constants and loaded its main classes.
*
* @since 0.2
*/
function yoast_wpseo_video_seo_init() {
if ( defined( ‘WPSEO_VERSION’ ) ) {
$wpseo_video_xml = new wpseo_Video_Sitemap();
} else {
add_action( ‘all_admin_notices’, ‘yoast_wpseo_missing_error’ );
}
}
/**
* Throw an error if WordPress SEO is not installed.
*
* @since 0.2
*/
function yoast_wpseo_missing_error() {
echo ‘</pre>
<div class="error">
Please <a href="’ . admin_url( ‘plugin-install.php?tab=search&type=term&s=wordpress+seo&plugin-search-input=Search+Plugins’ ) . ‘">install & activate WordPress SEO by Yoast</a> and then enable its XML sitemap functionality to allow the Video SEO module to work.</div>
<pre>
‘;
}
[/php]
Genesis Connect for Easy Digital Downloads
Here’s a different approach by an addon plugin that relies on both the Genesis theme and the Easy Digital Downloads plugin to be installed, before it can do its job:
[php]
/** Check for activated Easy Digital Downloads plugin */
if ( ! is_plugin_active( ‘easy-digital-downloads/easy-digital-downloads.php’ ) ) {
$gcedd_message .= sprintf( ‘
%s’, __( ‘Install and activate the Easy Digital Downloads plugin.’, ‘genesis-connect-edd’) );
} // end-if
/** Check for activated Genesis Framework (= template/parent theme) */
if ( basename( get_template_directory() ) != ‘genesis’ ) {
$gcedd_message .= sprintf( ‘
‘ . __( ‘Sorry, you canβt activate unless you have installed the %1$sGenesis Framework%2$s’, ‘genesis-connect-edd’ ), ‘<a href="http://deckerweb.de/go/genesis/" target="_new">’, ‘</a>’ );
} // end-if
/** Populate activation error messages and plugin-deactivation if neccessary */
if ( ! empty( $gcedd_message ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) ); /** Deactivate ourself */
$gcedd_message = __( ‘Sorry! In order to use the Genesis Connect for Easy Digital Downloads plugin you need to do the following:’, ‘genesis-connect-edd’ ) . $gcedd_message;
wp_die( $gcedd_message, __( ‘Genesis Connect for Easy Digital Downloads Plugin’, ‘genesis-connect-edd’ ), array( ‘back_link’ => true ) );
} // end-if
[/php]
TGM Plugin Activation
TGM Plugin Activation is worth a mention because it goes one step further than outputting an admin warning, but actually lets the user install the plugin in a semi-automated fashion. It’s a class that can be bundled with your theme or plugin.
TGM Plugin Activation is a PHP library that allows you to easily require or recommend plugins for your WordPress themes (and plugins). It allows your users to install and even automatically activate plugins in singular or bulk fashion using native WordPress classes, functions and interfaces. You can reference pre-packaged plugins, plugins from the WordPress Plugin Repository or even plugins hosted elsewhere on the internet.
TGM Plugin Activation isΒ available on GitHub.
Do you know of any other methods to achieve this functionality? Let us know in the comments section below!
If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.