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.

OK, I can create the code to add custom widgets to the widgets manager. Check.

My next challenge is to, in the same plugin, during activation, check for the existence of my theme, and if active, pre-populate the widget areas with the custom widgets I've just created.

I hope this makes sense. If I can do this, it will save a ton of time from configuring (and explaining how to configure) my default theme setup, which relies heavily on custom widgets. Things like an optin email form, custom slider widget, etc...

Thanks in advance if anyone can help me here.

Here are my theme's default sidebars...

if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'name' => 'Home Sidebar',
        'id' => 'home-sidebar-widget',
    ));

if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'name' => 'Home Header Area',
        'id' => 'home-header-widget',
    ));

if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'name' => 'Inside Header Area',
        'id' => 'inside-header-widget',
    ));

if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'name' => 'Home Footer Area',
        'id' => 'home-footer-widget',
    ));

if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'name' => 'Inside Footer Area',
        'id' => 'inside-footer-widget',
    ));

if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'name' => 'Inside Sidebar',
        'id' => 'inside-sidebar-widget',
    ));
share|improve this question
    
Scott, based on toscho's answer and comment, do you have a working code example? –  brasofilo Jul 20 '12 at 10:11
add comment

1 Answer

up vote 1 down vote accepted

Sidebars are registered in the option 'sidebars_widgets'. To see what’s in and what not, try this:

add_action( 'wp_footer', 'show_my_sidebars' );

function show_my_sidebars()
{
    $sw = get_option( 'sidebars_widgets' );
    print '<pre>' . htmlspecialchars( print_r( $sw, TRUE ) ) . '</pre>';
}

Sample output:

Array 
(
[sidebar-footer-left] => Array 
( 
[0] => archives-3 
)
[sidebar-footer-right] => Array 
( 
[0] => recent-comments-3 
)[array_version] => 3 
)

So check the option in per activation hook and insert your widgets if they aren’t already in there.

And stop using if ( function_exists('register_sidebar') ). Do you really want to support four years old WP versions?

share|improve this answer
    
Thanks toscho. Point taken on the function_exists wrapper. I appreciate your help :-) –  Scott B Mar 13 '11 at 5:02
    
Assuming I'm creating the widgets during the plugin activation, I can also insert each of them into a named sidebar? –  Scott B Mar 13 '11 at 6:33
    
I haven’t done this yet. :) It is just the way I would try first. If you write a working code example it would be awesome if you’d add it to your question or on GitHub. –  toscho Mar 13 '11 at 16:37
add comment

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.