How are Dynamic Sidebar Widgets rendered

I have been reading through the WordPress Source, trying to get a better understanding of how dynamic sidebars are rendered.

However, I am hitting a sticking point…

Read More
894 | do_action( 'dynamic_sidebar', $wp_registered_widgets[$id] );

I can’t find where add_action(‘dynamic_sidebar’, … ) is defined. Without out that part, I am sort of lost in what happens.

See the code here:

https://github.com/WordPress/WordPress/blob/b7c13e27c255e1fc1f03ab2ab432f1652a0ac212/wp-includes/widgets.php#L894

And to give more context, I am trying to figure out how to grab an array of widgets, and then from there, how would you render each one.

I need finer control than dynamic_sidebar(...); gives me

Related posts

1 comment

  1. That action is just there in case a plugin or theme wants to do something before sidebars are rendered. The code you’re looking for is in widgets.php, just before and just after that action:

    $callback = $wp_registered_widgets[$id]['callback'];
    
    do_action( 'dynamic_sidebar', $wp_registered_widgets[$id] );
    
    if ( is_callable($callback) ) {
        call_user_func_array($callback, $params);
        $did_one = true;
    }
    

    In the wp_register_sidebar_widget() function, you can see where $wp_registered_widgets[$id] gets set to an array of information about the widget, including the display/output callback:

    $widget = array(
        'name' => $name,
        'id' => $id,
        'callback' => $output_callback,
        'params' => array_slice(func_get_args(), 4)
    );
    $widget = array_merge($widget, $options);
    
    if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) ) {
        do_action( 'wp_register_sidebar_widget', $widget );
        $wp_registered_widgets[$id] = $widget;
    }
    

Comments are closed.