0

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',
    ));
  • Scott, based on toscho's answer and comment, do you have a working code example? – brasofilo Jul 20 '12 at 10:11
1

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?

  • 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. – fuxia Mar 13 '11 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.