How to get widget options?
I’ve tried like this, like seen in the other question “Using widget options outside of the widget”:
$w=get_option('widget_wp_mywidget');
$one=$w[2]["myval"];
$two=$w[2]["myotherval"];
But I am not quite sure why this [2] is dynamic and i can’t rely on it, so how to get exact values of the array the right way?
Widget data is stored in the options table as a two dimensional array. All data for the same type of widget is stored under the same option key. The key of the outer array is a kind of widget “index”– the identifier for the particular instance of the widget. The inner arrays contain the individual widget data. Like so:
To see what I mean, place the following in your theme header (or somewhere easily visible):
Then drag your widget into the sidebar. You should see that
$w
array get larger. If you delete some widgets you will notice that the keys don’t reset. They are not simple auto-increment numeric indexes.What this means is to pull the widget data for a particular widget, you need to know the widget’s instance identifier, which is really pretty tricky and I don’t know exactly what your requirements are.
You could crawl that
$w
array looking for a clue like a title, or crawl the site sidebars looking for a widget in a particular location. For example, try:You will notice that there is data in that array of the pattern
text-7
,pages-4
. That prefix is the widget type and the suffix matches the “identifiers”– the keys– in the array above.Edit:
After more thought and several comments, I think the best answer is:
There is no best way to get widget options. Widgets are intended to operate within and to be managed by the rather complicated Widget API built into the Core. There is no easy way, and certainly no Core way, to get access to widget data outside of that system. A lot of information has to get juggled around to take widgets apart and to put them back together.
My feeling is that if you think you have the need to use a widget outside of the Widget API you are probably using the wrong tool for the job.
I can understand the need, perhaps, to use the same data in a widget and elsewhere but I would suggest that using the widget as the source of that data is backwards. The more I think about this the more I am convinced that the better way to do it is to use some other form/interface for the data– Theme Option, Plugin Settings page, something– and then retrieve that data for use in the widget and outside of it.
First you need to know the name of the $widget.
In this example our widget’s class name is FooBar.
Refer to WordPress Codex or, plugin docs for the widget class names. https://codex.wordpress.org/Function_Reference/the_widget
Next we want to get $all_instances of the widget.
Widgets are stored as options in the database.
We can use the ‘get_option()’ WordPress function to retrieve all the widget data associated with a particular widget class.
Widget option names use the prefix ‘widget‘ so, your option name will look something like: ‘widget_foobar‘
Let’s print an array of all the widgets stored in that option
Now let’s get the widget $instance we want
We’re going to print the 1st widget in the array which has an index of [2].
Omit $default_args to use the theme’s default widget ‘before and after‘ arguments.