Using after_switch_theme
will activate the theme (which is fine as we want to run the check within the context of the new theme). So if the check fails, we can simply switch back to the previous theme (passed via after_switch_theme
as $oldtheme
).
If WPML is missing ($wpml_is_missing = true;
) we will output an admin notice and switch back using switch_theme()
like this:
add_action( 'after_switch_theme', 'check_required_theme_plugins', 10, 2 );
function check_required_theme_plugins( $oldtheme_name, $oldtheme ) {
if ( $wpml_is_missing ) :
// Info message: Theme not activated
add_action( 'admin_notices', 'not_activated_admin_notice' );
function not_activated_admin_notice() {
echo '<div class="update-nag">';
_e( 'Theme not activated: this theme requires WPML.', 'text-domain' );
echo '</div>';
}
// Switch back to previous theme
switch_theme( $oldtheme->stylesheet );
return false;
endif;
}
You can use the snippet above within your functions.php
, but make sure that after_switch_theme
is called before WPML is required.