Support » Developing with WordPress » How to abort the activation of a theme?

  • ioclaudio



    I’ve created a custom theme. I’d like that the activation of this theme doesn’t start if a certain plugin is not yet installed.

    I know how to check for the presence of this plugin, but I don’t know if there is an hook that allows to abort the activation of a theme.

    How can I stop the activation of the theme and show an alert message on the wordpress dashboard?

    Thank you very much


Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    You cannot execute theme code unless it is activated, even it is to abort activation, so you’d really be activating, checking for the plugin, then switching back if the check fails.

    Untested, but I think you could use the “after_switch_theme” hook to execute such code and call switch_theme() to restore the previous theme if the check fails.

    The problem I see is in putting up an alert if it’s to be the typical WP message box. When you switch back to the old theme, such a message would disappear when the page reloads. The message would need to be a true alert box that requires user interaction before proceeding. It would in fact initiate a restore old theme theme request. If the user should ignore it and simply close the window, your code would not know to switch back.

    Your theme could maybe save the old theme’s reference when the plugin check fails. If the setting still exists on the next request, via an “init” action callback, you could then force switch back to that previous theme after first removing the reference. If the user does acknowledge the message, remove the reference and switch back immediately. Then the “init” callback wouldn’t be needed.

    Moderator Jose Castaneda



    Random thought here is you could also look at the core themes and see how they handle version comparison to get an idea of how to do that too and maybe use the function is_plugin_active() to check.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.