Output dynamic_sidebar_params in wp_head

Going through its normal proceedings WordPress will already have evaluated wp_head by the time it gets to the widgets. The dynamic_sidebar_params filter will only be called then as well, so it will also be of little help to get your css in the head. Here‘s something I wrote about this issue earlier.

To include information from widget settings in the head, you would have to access the settings directly with a function that is activated on the init action. Beware that I have not tested the following and don’t know if any complications could arise.

All widget settings are stored in a global variable called $wp_registered_widgets, which is sadly not very well documented (presumably because you’re not supposed to mess with it). The datastructure is something like this (props @shibashake, but it might be changed without warning):

Array (
    [archives-1] => Array (
            [name] => Archives
            [id] => archives-1
            [callback] => Array (
                    [0] => WP_Widget_Archives Object (
                            [id_base] => archives
                            [name] => Archives
                            [widget_options] => Array (
                                    [classname] => widget_archive
                                    Output dynamic_sidebar_params in wp_head => A monthly archive of your blog’s posts )

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

                            [number] => 1
                            [id] => archives-1
                            [updated] => 
                            [option_name] => widget_archives )

                    [1] => display_callback )

            [params] => Array (
                    [0] => Array (
                            [number] => -1 ) 
                )

            [classname] => widget_archive
            Output dynamic_sidebar_params in wp_head => A monthly archive of your blog’s posts )

    [text-8] => Array (
            [name] => Text
            [id] => text-8
            [callback] => Array (
                    [0] => WP_Widget_Text Object (
                            [id_base] => text
                            [name] => Text
                            [widget_options] => Array (
                                    [classname] => widget_text
                                    Output dynamic_sidebar_params in wp_head => Arbitrary text or HTML )

                            [control_options] => Array (
                                    [id_base] => text
                                    [width] => 400
                                    [height] => 350 )

                            [number] => 8
                            [id] => text-8
                            [updated] => 
                            [option_name] => widget_text )

                    [1] => display_callback )

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

            [classname] => widget_text
            Output dynamic_sidebar_params in wp_head => Arbitrary text or HTML )
... )

Now, you could access this datastructure:

  1. Search through the array to find all instances of your widget

  2. Inside each instance find the style settings you have recorded and output them using wp_add_inline_style.