3

I am registering sidebars and sidebar widgets.

The theme currently supports two sidebars. Primary and Secondary.

add_action('widgets_init', array($this, 'add_sidebars'), 10, 2);

public function add_sidebars(){
    register_sidebar(array(
        'name' => 'Primary Sidebar',
        'id' => 'mbe-sidebar-primary-sidebar',
        'description' => '',
        'class' => 'mbe-sidebar',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>'
    ));
    register_sidebar(array(
        'name' => 'Secondary Sidebar',
        'id' => 'mbe-sidebar-secondary-sidebar',
        'description' => '',
        'class' => 'mbe-sidebar',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>'
    ));
    $this->add_sidebar_widgets();
}

Then I add all the preset widgets (post objects from a post type) as an available sidebar widget.

private function add_sidebar_widgets(){
    global $mbe_content;
    $widgets = $mbe_content->get_content('archive', 'mbe-sidebar-widgets');
    if(!$widgets){
        return;
    }
    foreach($widgets as $widget){
        wp_register_sidebar_widget(
            'mbe-sidebar-widget-'.$widget['post_name'],
            $widget['post_title'],
            array($this, 'display_widget'),
            array(
                'description' => 'Sidebar Widget'
            ),
            ''
        );
    }
}

public function display_widget($args, $params){
    echo 'widget';
}

I have also set those sidebar widgets as active widgets in the primary sidebar. Now for example, at a theme level, If I wanted to just use a dynamic sidebar...

if(is_active_sidebar('mbe-sidebar-primary-sidebar')){
    dynamic_sidebar('mbe-sidebar-primary-sidebar');
} else{
    echo 'NO PRIMARY WIDGETS!'.PHP_EOL;
}

It show's "widget" from my widgets, but that's where the output for my widgets will occur. So that doesn't matter. The point is, the dynamic sidebar is working, and I can see my widget outputter.

A question I'm having is, how come if I add a simple default WordPress widget to the sidebar, it doesn't output anything on the sidebar? It's like it doesn't even exist or something.

How can I get my widgets to output on the sidebar, while still allowing the default sidebar widgets provided by WordPress to output on the sidebar as well?

I originally thought it was my function display_widget() So I tried getting all widgets, looping through them, then calling the_widget() on each of their widget ID's, but then I'm getting errors about that widget not being found, blah blah.

4
  • What is the right way to automatically display each of the active sidebar widgets? using dynamic_sidebar() Mar 5, 2013 at 17:15
  • Does all of your code appear in your functions.php file?
    – Ryan
    Mar 6, 2013 at 22:12
  • The sidebar and widget code is fired on the action: widgets_init, the call for dynamic_sidebar() is fired on the action: template_redirect to another custom hook in the theme's sidebar.php file. Mar 11, 2013 at 19:29
  • I am not sure what you're question is. WordPress doesn't populate sidebars with any widgets, so far as I am aware. Your theme might though.
    – s_ha_dum
    Dec 1, 2013 at 17:59

1 Answer 1

1

Check your template file from your theme. There may be some widgets being added in there manually if no other widgets are enabled.

If that's the case modify the conditional statement in your sidebar template so your widgets are output after the defaults set there.

@s_ha_dum basically got to the bottom of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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