I am working on a theme that runs code only once on theme activation (using the after_switch_theme hook). I am wondering if it's possible to safely prevent my theme from being activated if certain requirements are not met.

Specifically, I want to check if WPML is installed and activated or not, and if not, prevent my theme from being activated. Checking if WPML is activated is not an issue, my issue is how to safely stop my theme from being activated. I am afraid that using PHP's die() might cause issues with a half activated theme. Even if die() were to work properly, I would still need to activate another theme and then activate WPML, and finally then reactivate my theme.

Here's what the code would ideally look like:

add_action('after_switch_theme', 'theme_custom_install');
function theme_custom_install() {
    if( $wpml_is_missing ) {

Is there a native WP way to do this, or does a plugin exist that does this?


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;



You can use the snippet above within your functions.php, but make sure that after_switch_theme is called before WPML is required.


Not the answer you're looking for? Browse other questions tagged or ask your own question.