WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
up vote 1 down vote accepted

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

share|improve this answer
if the theme is being initialized from a plugin, won't $pagenow = 'themes.php' always be false? – Scott B Mar 31 '11 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. – rofflox Mar 31 '11 at 15:08
+1 Great so far. Could I use if(!$initialized && is_admin()) instead of if(false===$initialized) ? – Scott B Mar 31 '11 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. – rofflox Mar 31 '11 at 15:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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