Set options on activation Themes

The Photo Blog Theme Greyfoto has been updated and I wanted to make sure that the settings of WordPress are as I need them for the theme. Therefore must be written some data in the database when activate. Currently there is no hook for this, as is known for Plugins. The discussion on WP-Hacker-List was just right on time, and possibly Theme Developers get these hooks, similar to Plugins in WordPress 2.9.

If you want to use the activation of the theme in order to set various settings, then the following snippet can be very useful, which came up in the discussion on the WP-Hacker-List. This works like a charm and should be useful for theme developers who want to enable the options upon activation.

Here’s a small example in which the default setting posts_per_page of WordPress in the table options is reset to the value 1.

// with activate istall option
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {
	update_option( 'posts_per_page', 1 );
}

Since 2.9 you can use this function: register_theme_directory


Posted

in

by

Comments

13 responses to “Set options on activation Themes”

  1. Mael Avatar

    Some developpers also develop their theme as a plugin;

  2. Home Trends Avatar

    I love this tips… I will try on my site

  3. Octav Avatar

    wow, this sounds really interesting, thanks for the tip!

  4. N-Designs Avatar

    thanks for the tip. will try it..

  5. David Cowgill Avatar

    This is a very good tip. I also like to check and make sure the option doesn’t already exist before clobbering it by accident. To do this make sure you run a quick if statement before each update_option like so.

    if(get_option(‘cp_num_images’) == false) { update_option(‘cp_num_images’, 3); }

  6. Keith Avatar
    Keith

    Thanks for publishing. I was looking for a good work around for the lack of an activation hook for themes.

    David,

    To avoid overwriting, a better approach may be to use add_option instead of update_option as add_option will not overwrite if a value already exists.

  7. philip Avatar
    philip

    thanks a lot for this,
    can i ask if someone here knows how to pre-add some pages and categories after theme activation?

    thanks a lot for your help!

  8. Philip Avatar
    Philip

    thanks a lot Michael,

    i have search in the options.php page and i can’t find any information about categories and pages,

    i think i have to do something different,

    but thanks a lot for your help

  9. Jeff Ballweg Avatar

    Just implementing the same workaround right now… has a formal activation hook been added in WP3.0? – I found a thread on nabble from about a year ago that seems to indicate that there still isn’t one in the core.

  10. Dion Hulse (dd32) Avatar

    You can also get a deactivation hook by catching the user switching the theme, For example, i used this in some code a long time ago:


    add_action('switch_theme', 'theme_deactivate');
    function theme_deactivate(){
    delete_option('my_options_that_dont_matter');
    }

  11. Michael Avatar

    @Dion Hulse:Thanks for the tip!

  12. Krishna Kant Sharma Avatar

    I was looking for activation/deactivation hook for themes. Yours was the best approach but it is a bit unreliable as admin can refresh the page after activating theme and activation code can run multiple times. I have written a code which provides a reliable activation/deactivation theme hooks. Please check it out and let me know what you guys think!

    http://www.krishnakantsharma.com/2011/01/activationdeactivation-hook-for-wordpress-theme/