• I am stuck with something in WordPress. I was looking for the answer for this question everywhere possible but couldn’t find what I wanted to know.

    I have made a widget in the WordPress admin widget page and I want to use this widget data somewhere else in the website. I used the function $data = get_option('widget_data'); to retrieve the data of the widget. Since WordPress supports multiple instances of a widget, the values are stored in database as an array and the output of the code gave me this array list :

    array (size=4)
      2 =>
        array (size=3)
          'title' => string 'Notice 0' (length=14)
          'page_id' => string '9472' (length=4)
          'desc' => string '1' (length=1)
      3 =>
        array (size=3)
          'title' => string 'Notice 1' (length=14)
          'page_id' => string '9594' (length=4)
          'desc' => string '1' (length=1)
      4 =>
        array (size=3)
          'title' => string 'Notice 2' (length=16)
          'page_id' => string '9594' (length=4)
          'desc' => string '1' (length=1)
      '_multiwidget' => int 1

    The problem is: When I remove an instance of the widget from the widget holder the data used by that deleted widget is not flushed out and because of this I can’t understand whether or not to use the data data from array.

    My question is: Is there anyway to understand whether the widget instance is still active or not? As you can see that there is no indication or value to understand which all instances are active at the moment and which are not active. Please guide me..

    Warm Regards 🙂

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The widget has to have some way of knowing which array element belongs to it. It’s probably the index number at the highest level in the array.

    I’m guessing this is the $widget->number value assigned to each instance. It appears this number is also the numeric part of the widget’s HTML content ID. In the page source where the widget appears, you should see an <aside> element with an ID like “categories-3”. I believe that “3” corresponds to $widget->number and the array index in the saved values for that widget type.

    I haven’t positively confirmed any of this, but this is how it appears to me and seems like a good direction for further investigation. Good luck!

Viewing 1 replies (of 1 total)

The topic ‘Filter out the active widget array list – WordPress’ is closed to new replies.