2

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

为了提供更多上下文,我试图弄清楚如何从特定侧边栏中获取一组小部件,然后从那里,我需要知道如何渲染该数组中的每个小部件。

我需要比dynamic_sidebar(...);给我更好的控制

4

1 回答 1

1

好吧,该特定行允许您访问每个已注册的 Widget 属性,它的用法如下:

<?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
    )
    */
}

WordPress Answers 上的相关搜索查询

于 2013-09-06T20:37:57.737 回答