2
votes

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...

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

I can't find where add_action('dynamic_sidebar', ... ) is defined. Without 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 from a specific sidebar, and from there, I need to know how would you render each widget within that array.

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

1
Seems that you have 3 questions in one: 1) What the line 894 does. 2) Grab widgets from specific sidebar. 3) Render this widgets your own way. . . . I suspect 1 won't help with 2 and 3, and maybe they deserve separate Questions and research. - brasofilo

1 Answers

1
votes

Well, that specific line permits you to access each registered Widget properties, and it's used like:

<?php
/* Plugin Name: Test registered widgets */

add_action( 'dynamic_sidebar', 'sidebar_widgets_so_18666065' );

/**
 * As this is an action hook, we don't return nothing
 * use the passed values to do your stuff
 */
function sidebar_widgets_so_18666065( $registered_widget )
{
    # Each registered widget passes the following array
    /*
    $registered_widget = Array
    (
        [name] => Meta
        [id] => meta-2
        [callback] => Array
            (
                [0] => WP_Widget_Meta Object
                    (
                        [id_base] => meta
                        [name] => Meta
                        [widget_options] => Array
                            (
                                [classname] => widget_meta
                                [description] => Log in/out, admin, feed and WordPress links
                            )

                        [control_options] => Array
                            (
                                [id_base] => meta
                            )

                        [number] => 2
                        [id] => meta-2
                        [updated] => 
                        [option_name] => widget_meta
                    )

                [1] => display_callback
            )

        [params] => Array
            (
                [0] => Array
                    (
                        [number] => 2
                    )

            )

        [classname] => widget_meta
        [description] => Log in/out, admin, feed and WordPress links
    )
    */
}

Relevant search query at WordPress Answers.