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.
dynamic_sidebar()
widgets_init
, the call fordynamic_sidebar()
is fired on the action:template_redirect
to another custom hook in the theme'ssidebar.php
file.