1

I'm able to display a message by activation of my theme with the 'new' after_switch_theme hook this way:

function themeActivationFunction($oldname, $oldtheme=false) {
$msg = '
    <div class="error">
        <p>The ' . get_option( 'current_theme' ) . ' theme has been ACTIVATED.</p>
    </div>';
    add_action( 'admin_notices', $c = create_function( '', 'echo "' . addcslashes( $msg, '"' ) . '";' ) );

}
add_action("after_switch_theme", "themeActivationFunction", 10 ,  2);

But it does not work with the swith_theme hook for deactivation of my theme. I think because by switching the theme to another one to default WordPress message overwrites my message stucking in the theme that gets deactivated and the reload of the screen deletes everything.

function themeDeactivationFunction($theme) {
// Lets let the admin know whats going on.
        $msg = '
    <div class="error">
        <p>Your theme has been DEACTIVATED</p>
    </div>';
    add_action( 'admin_notices', $c = create_function( '', 'echo "' . addcslashes( $msg, '"' ) . '";' ) );
}
add_action("switch_theme", "themeDeactivationFunction", 10 , 2);

I'm looking for a 'work around' to notify a user about deactivating my theme and the setup routine after it.

1
  • The messages are just examples. Commented Aug 6, 2012 at 8:51

1 Answer 1

0

It's impossible, because after deactivation your theme isn't even loaded!

It's possible, but hacky. Essentially, we unset the action param, load in the themes admin page & then exit before the redirect-on-success occurs.

add_action( 'switch_theme', 'wpse_60972_theme_deactivate_message' );
function wpse_60972_theme_deactivate_message()
{
    $msg = '
    <div class="error">
        <p>Your theme has been DEACTIVATED</p>
    </div>';

    add_action( 'admin_notices', create_function( '', 'echo "' . addcslashes( $msg, '"' ) . '";' ) );

    unset( $_GET['action'] );
    require ABSPATH . 'wp-admin/themes.php';
    exit;
}

If the code you posted is verbatim, may I ask why you want do as such? WordPress has it's own notices for activation & deactivation.

9
  • I have a specific theme that should have specific pages. By activating the theme i check if there are those specific pages. If not, i create them and set some options like show_on_front, page_on_front and page_for_posts. After deactivating my specific theme i want to set the pervious options and notify the user about it. Commented Aug 6, 2012 at 8:47
  • That's fine, but why are the activation/deactivation messages necessary? Commented Aug 6, 2012 at 8:48
  • For good UX... feedback is always good for user. :-) Commented Aug 6, 2012 at 8:49
  • WordPress displays activation/deactivation messages by default, there's no need for you to replicate this behaviour. Commented Aug 6, 2012 at 8:52
  • Yes, i know. But for activation i want something like My-Theme is activated. Specific pages 'Wellcome' and 'News' were created. 'Wellcome' is now your front page and 'News' your posts page. Your previous settings were saved and will be reset by deactivating this theme. And for deactivation i want something like My-Theme is deactivated. Your previous settings are reseted. You can delete 'Wellcome' and 'News' by clicking here. Commented Aug 6, 2012 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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