I've got some code inside functions.php which is designed to execute only when the theme is first activated:

if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {
//this code only runs when the theme is first activated

However, I'm pretty sure this code is not running if the theme is activated outside the normal activation process. For example, if a switch_theme() statement is called from a plugin.

In that case, how might I alter my code above to execute on switch_theme()?

if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php') OR (switch_theme_called() ) ) {
//this code only runs when the theme is first activated

1 Answer 1


Well, instead of using a $_GET parameter you could store a initiate state in your options-table.


$initialized = get_option('mytheme_initialized');
if ( (false === $initialized) && is_admin() && ($pagenow == 'themes.php') ) {
//this code only runs when the theme is first activated
update_option('mytheme_initialized', true);

Unfortunately the "register_activation"-hook is only available for plugins -> http://core.trac.wordpress.org/ticket/13602

  • if the theme is being initialized from a plugin, won't $pagenow = 'themes.php' always be false?
    – Scott B
    Mar 31, 2011 at 14:56
  • You're right. You could just remove everything in the if statement and only check for the $initialized variable. This should do the trick. The first time the functions.php is called, the variable is set to true.
    – Roman
    Mar 31, 2011 at 15:08
  • +1 Great so far. Could I use if(!$initialized && is_admin()) instead of if(false===$initialized) ?
    – Scott B
    Mar 31, 2011 at 15:17
  • With if(false === $var) you also test if the type of the variable is a boolean. If you use if($var) instead, the if-statement could also be true if a numeric 1 or string 'true' is submitted. But yes, the script will work with your adjustments.
    – Roman
    Mar 31, 2011 at 15:30

Your Answer

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

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