Set options on activation Themes
September 21st, 2009 by Frank • WordPress Hacks • 6 Comments
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 ); }
Info
- Published in WordPress Hacks
- Tags: Code, hack, The, Theme, WordPress, WordPress Themes, WP
- Comment feed | Trackback URL
- read: 3033 | today: 13
- leave a Comment



Some developpers also develop their theme as a plugin;
I love this tips... I will try on my site
wow, this sounds really interesting, thanks for the tip!
thanks for the tip. will try it..
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); }
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.