0

I have created a custom widget with a color picker field. Currently there are two ways to output the color picker value for this widget:

  1. Inline style.
  2. Using the dynamic_sidebar_params filter. With this option i can output the color value inside the style tags, but it will be outputted inside the widget HTML.

Both methods have their own well known disadvantages.

The best solution would be to use the dynamic_sidebar_params filter and make the output in the head part of the document using the wp_head action. The thing is that i can't find a way to make it happen.

Have someone worked with a similar situation?

  • This is a common issue. Once WP starts evaluating widgets, the html for the head already has been generated. You cannot add anything anymore using wp_head. What you could do is have your plugin read the widget settings and output them in the head, before the evaluation of the actual widget class. – cjbj Jun 24 '16 at 12:20
  • Thank you. I tried it as well, find the widget id and pass it via the wp_head action, but still no success. Could you please share a an example of such solution, if you have one? – Vasili Jun 24 '16 at 12:33
0

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
                                    [description] => 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
            [description] => 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
                                    [description] => 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
            [description] => 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.