WordPress Activate Theme Action

There isn’t yet a WordPress activate theme hook. In the last week, it’s come up twice where WordPress.com Hosting VIP partners wanted some code to run once on theme activation.

It’s not an unusual scenario for our customers to create a new version of a theme, install it separately, and then activate it. Often this also allows reverting to the old version of the theme if something unexpected happens at launch.

In this scenario, it’s often easy to check for the existence of a new option, migrated, or other seed data, but sometimes you want to do something like:

global $pagenow;
if ( is_admin() && 'themes.php' == $pagenow && isset( $_GET['activated'] ) ) {
     // When theme is activated this code runs.
     // Still be defensive if you need to be, and check if
     // your baby is already born
}

Hat tip Frank Bültge.