Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
What is the right way to automatically display each of the active sidebar widgets? using dynamic_sidebar() – Michael Ecklund Mar 5 at 17:15
Does all of your code appear in your functions.php file? – Ryan Mar 6 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. – Michael Ecklund Mar 11 at 19:29

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.