1

Is it possible to return a widget instance into a variable or array? I'm thinking something along the lines of $widget['title'], $widget['content'] etc.

I have tried inspecting the global $wp_registered_widgets variable but this only seems to contain information relevant to rendering the widget, not the widget content itself. I am also able to retrieve the rendered widget by using

ob_start(); 
dynamic_sidebar('widgetarea'); 
$sidebar = ob_get_contents();    
ob_end_clean();

Which I suppose I could then parse into a series of dom nodes with jQuery parseHTML method, however this seems like a very messy way to go about it and I wonder if anyone could suggest a more elegant way?

3
  • This might help. I don't have time to write a proper answer :)
    – s_ha_dum
    Commented Jun 2, 2014 at 15:57
  • I did notice that answer - although forgive me my ignorance, as I say in the question, inspecting the global $wp_registered_widgets variable for the widget I wish to retrieve the data for, only seems to contain meta information for the widget, not the widget content itself? Commented Jun 2, 2014 at 16:03
  • Widgets are not easy to deal with outside of the Core mechanisms. You need to go through several steps and deal with a couple of arrays. Follow the code in that question carefully. Run the code and dump the data at different points to try to see what is happening.
    – s_ha_dum
    Commented Jun 2, 2014 at 16:19

1 Answer 1

0

Where are the widgets settings stored?

The widget settings are stored in the wp_options table. You can retrieve it with

print_r( get_option( 'widget_x' ) );

where x is the $id_base input parameter of the WP_Widget class constructor. If $id_base is empty, then it uses the widget class name in lower case letters, without the prefixes: wp_widget or widget_.

Here's an example:

class WP_Widget_Categories extends WP_Widget {

        function __construct() {
                $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories." ) );
                parent::__construct('categories', __('Categories'), $widget_ops);
        }

You can see how the corresponding option name is created, here in the WP_Widget class constructor:

function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
            $this->id_base = empty($id_base) ? preg_replace( '/(wp_)?widget_/', '', strtolower(get_class($this)) ) : strtolower($id_base);
            $this->name = $name;
            $this->option_name = 'widget_' . $this->id_base;
            $this->widget_options = wp_parse_args( $widget_options, array('classname' => $this->option_name) );
            $this->control_options = wp_parse_args( $control_options, array('id_base' => $this->id_base) );
    }

where the save settings method is:

   function save_settings($settings) {
            $settings['_multiwidget'] = 1;
            update_option( $this->option_name, $settings );
    }

Example:

Widget class name      | Option name
------------------------------------------
WP_Widget_Text         | widget_text
WP_Widget_Archives     | widget_archives
WP_Widget_Calendar     | widget_calendar
WP_Widget_Search       | widget_search    
WP_Widget_Tag_Cloud    | widget_tag_cloud
WP_Widget_Categories   | widget_categories
WP_Widget_Recent_Posts | widget_recent-posts
My_Widget              | widget_my_widget

... etc

For multi widgets the option value is an array with instance numbers as array keys.

Example: Text widgets

To retrieve all the saved data from the text widgets, we can use:

print_r( get_option( 'widget_text' ) );

and get for example the following output:

Array(

   [1] => Array
        (
            [title] => Bacon Ipsum
            [text] => Bacon ipsum dolor sit amet t-bone shankle landjaeger, turkey meatloaf shank swine jowl jerky doner kielbasa salami ham. 
            [filter] => 
        )
   [3] => Array
        (
            [title] => Lorem Ipsum
            [text] => Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
            [filter] => 
        )

    [_multiwidget] => 1
)

I hope this helps.

1
  • 1
    That more than helps, that is exactly what I was after I just didn't have a clear enough understanding of how widgets are saved to the database to know how to get that result. Thanks very much for taking the time to answer! Commented Jun 2, 2014 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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