Earlier this week I talked about how you can’t actually network activate all plugins. As it happens, I have a plugin for a specific theme, and it doesn’t like being Network Activated.
The plugin is Genesis Simple Hooks and, logically, it only works if you have a Genesis theme installed and active.
How It Works
When the plugin is activated it properly runs the following activation hook:
register_activation_hook( __FILE__, 'simplehooks_activation' ); /** * This function runs on plugin activation. It checks to make sure Genesis * or a Genesis child theme is active. If not, it deactivates itself. * * @since 0.1.0 */ function simplehooks_activation() { if ( ! defined( 'PARENT_THEME_VERSION' ) || ! version_compare( PARENT_THEME_VERSION, '2.1.0', '>=' ) ) simplehooks_deactivate( '2.1.0', '3.9.2' ); }
First this looks to see if there’s no parent theme or if the parent theme version is less than 2.1.0, and if either of those are the case (that is if there’s no parent theme, and the version is less than…) it calls simplehooks_deactivate
.
/** * Deactivate Simple Hooks. * * This function deactivates Simple Hooks. * * @since 1.8.0.2 */ function simplehooks_deactivate( $genesis_version = '2.1.0', $wp_version = '3.9.2' ) { deactivate_plugins( plugin_basename( __FILE__ ) ); wp_die( sprintf( __( 'Sorry, you cannot run Simple Hooks without WordPress %s and <a href="%s">Genesis %s</a>, or greater.', 'genesis-simple-hooks' ), $wp_version, 'http://my.studiopress.com/?download_id=91046d629e74d525b3f2978e404e7ffa', $genesis_version ) ); }
And you know what? That sucks. It never checks for Genesis as the parent theme name, based on the assumption that only Genesis themes use the define of PARENT_THEME_VERSION
which probably made sense a few years ago, but doesn’t anymore. And more importantly for this explanation, you can’t network activate it because unless you’re lucky enough to be running a Genesis child theme on the main site of your network, it won’t activate.
Worse, it will throw errors on sites that aren’t.
How I’d Fix It
First I made a list of the action/function calls that require a Genesis Child Theme to be active in order to run properly: simplehooks_load_textdomain
, simplehooks_init
, simplehooks_execute_hooks
Then I moved them all to their own section and wrapped them in a check:
if ( simplehooks_can_run() ) { add_action( 'plugins_loaded', 'simplehooks_load_textdomain' ); add_action( 'genesis_init', 'simplehooks_init', 20 ); add_action( 'genesis_init', 'simplehooks_execute_hooks', 20 ); }
What’s simplehooks_can_run
? That’s a new function I’ve created to check for the requirements:
/** * Can Simple Hooks Run * * This function checks if the requirements for Simple Hooks are met. * * @since 2.3.0 */ function simplehooks_can_run() { global $wp_version; $my_theme = wp_get_theme(); $genesis_theme = wp_get_theme( 'genesis' ); if ( ( version_compare( $genesis_theme->get( 'Version' ), '2.1.0', '>=' ) && !is_null( $genesis_theme->get( 'Version' ) ) && !empty( $genesis_theme->get( 'Version' ) ) ) && ( $my_theme->get( 'Name' ) == 'Genesis' || $my_theme->get( 'Template' ) == 'genesis' ) ) { return true; } return false; }
What this checks is if the parent theme is 2.1.0 or higher and, if it is, if the theme or it’s parent is named Genesis. By having it use return
I don’t have to check if it’s true or false, the if-check is smart enough for me not to do it explicitly.
Finally I changed the if check in simplehooks_activation
to look like this:
if ( !is_multisite() && !simplehooks_can_run() )
What this isn’t doing is checking for WordPress 3.9.x anymore, as it’s 2016 and that was 2014 and I’m not worried about it. If I was, I’d toss && version_compare( $wp_version, '3.9.2', '>=' )
to the if-check in simplehooks_can_use
to CYA.
This also isn’t giving you an error on Multisite. That means if you network activate the plugin and, on Multisite, go to a site that does not have Genesis running, you don’t get an error. This is by design. There’s no point in telling a site-admin “Hey there’s this Genesis thing you can’t have because you’re not using it! NEENER!”
I’m actually using this here on this site. If you’re interested at testing it out, grab it from my Github repo.
Pull requests welcome.
Comments
One response to “Multisite And Theme Activation Checks”
Nice clear explanation!
I’ll use similar logic for a custom post loop which I intend to use on Genesis and non-Genesis activated sites.
Thank you!