Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to preset some widgets using functions included in my theme ONLY after a site is initially created (my theme is the default for all new sites in my instance of WordPress).

I DO NOT want my preset widgets to replace existing widgets when a user activates my theme UNLESS their existing widget areas are different from those in my theme.

I've modelled my widgets_init function on the one found in the Thematic theme framework. Thematic adds preset widgets using the following conditional:

if ( isset( $_GET['activated'] ) )

Thus when Thematic is activated, it wipes out existing widgets and adds its own preset widgets... I want my theme to use existing widgets if they are in widget areas my theme supports (i.e. a user switches from one child theme to another, both of which use my theme as their parent)

I know there is register_activation_hook but that seems to be designed for plugin functions and not functions included in themes... Here are questions:

  1. Is there an action that is run ONLY after a site has been initially created?

  2. Alternatively are their conditions I can check to determine whether a site has just been created?

share|improve this question

1 Answer 1

Why not output your Theme's custom Widgets using the_widget(), inside of a if ( ! dynamic_sidebar( 'sidebar-name' ) ) conditional?

e.g.

<?php if ( !dynamic_sidebar( 'sidebar-right' ) ) {

$widgetsidebarrightargs = array(
    'before_widget' => '<div class="widget">',
    'after_widget' => '</div>',
    'before_title' => '<div class="title widgettitle">',
    'after_title' => '</div>'
);

the_widget('WP_Widget_Calendar' , 'title=' , $widgetsidebarrightargs );
the_widget( 'oenology_widget_linkrollbycat' , 'title=Oenology Links by Cat' , $widgetsidebarrightargs );
the_widget('WP_Widget_Meta' , 'title=Meta' , $widgetsidebarrightargs );

} ?>

This is what I use in my Theme. The oenology_widget_linkrollbycat Widget is a custom Theme Widget. Simply replace with your own Widget names.

This way, your Widgets are applied to the specified sidebar by default, unless and until the user adds his own Widgets to the sidebar via Dashboard -> Appearance -> Widgets.

share|improve this answer
    
Hi Chip, this is helpful, but not ideal. Main problem with using the_widget template tag for default widgets is that widgets added this way don't show up via Dashboard -> Appearance -> Widgets and disappear when users add their own widgets. This is confusing for new users... –  marpa Apr 28 '11 at 14:59
    
@chip-bennett, My previous comment seems to have been truncated... Ideally theme developers should be able to specify their own preset (i.e. default) widgets and these should be included in the same way as user-selected widgets and ONLY included when a site is initially created or when existing widgets don't map to a theme's registered sidebars. That said, I did use the_widget in my sidebar template files for now, see: (bit.ly/jlcT36) –  marpa Apr 28 '11 at 15:22
    
Figured out how to determine if a site has just been created by checking if there is a post with id=1 whose post_date is within minutes of the current time. –  marpa Apr 30 '11 at 20:07

Your Answer

 
discard

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.