Skip to main content
added 1 character in body
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

Widgets should only be manipulated by users, and in general they should not be deleted because there is no version control for them. Therefor I suggest you rethink first before reading further.

Widgets are located in two places in the DB, the sidebar_widgets option is an array of sidebars, indexed by the sidebar id. Each sidebar is represented by an array of widget identifiers.

The individual widget settings are located at an option called widget_{widget name} which is an array indexed by the widget id of the widget.

So to removed some widgets of a specific type you need to do something like

$sidebars = get_option(`sidebar_widgets''sidebar_widgets');
$widgets  = get_option('widget'.your widget base name);
foreach ($sidebars as $sidebar_id => $sidebar) {
  foreach ($sidebar as $key=>$widget_id) {
    if ($widget_id match the name pattern of the widget type) {
      $widget = $widgets[$widget_id];
      if ($widget needs to be deleted)
        unset($widgets[$widget_id]);
        update_option('widget'.your widget base name, $widgets);
        unset($sidebars[$sidebar_id][$key]);
        update_option(`sidebar_widgets', $sidebars);
      }
    }
  }
}  

obviously you can start with locating the widget id in the widget's option and then loop over the sidebar.

Obviously it is just meta code and some small details might need to be adjusted, but pay attention that the unset here is probably done in a not very safe way.

Widgets should only be manipulated by users, and in general they should not be deleted because there is no version control for them. Therefor I suggest you rethink first before reading further.

Widgets are located in two places in the DB, the sidebar_widgets option is an array of sidebars, indexed by the sidebar id. Each sidebar is represented by an array of widget identifiers.

The individual widget settings are located at an option called widget_{widget name} which is an array indexed by the widget id of the widget.

So to removed some widgets of a specific type you need to do something like

$sidebars = get_option(`sidebar_widgets');
$widgets = get_option('widget'.your widget base name);
foreach ($sidebars as $sidebar_id => $sidebar) {
  foreach ($sidebar as $key=>$widget_id) {
    if ($widget_id match the name pattern of the widget type) {
      $widget = $widgets[$widget_id];
      if ($widget needs to be deleted)
        unset($widgets[$widget_id]);
        update_option('widget'.your widget base name, $widgets);
        unset($sidebars[$sidebar_id][$key]);
        update_option(`sidebar_widgets', $sidebars);
      }
    }
  }
}  

obviously you can start with locating the widget id in the widget's option and then loop over the sidebar.

Obviously it is just meta code and some small details might need to be adjusted, but pay attention that the unset here is probably done in a not very safe way.

Widgets should only be manipulated by users, and in general they should not be deleted because there is no version control for them. Therefor I suggest you rethink first before reading further.

Widgets are located in two places in the DB, the sidebar_widgets option is an array of sidebars, indexed by the sidebar id. Each sidebar is represented by an array of widget identifiers.

The individual widget settings are located at an option called widget_{widget name} which is an array indexed by the widget id of the widget.

So to removed some widgets of a specific type you need to do something like

$sidebars = get_option('sidebar_widgets');
$widgets  = get_option('widget'.your widget base name);
foreach ($sidebars as $sidebar_id => $sidebar) {
  foreach ($sidebar as $key=>$widget_id) {
    if ($widget_id match the name pattern of the widget type) {
      $widget = $widgets[$widget_id];
      if ($widget needs to be deleted)
        unset($widgets[$widget_id]);
        update_option('widget'.your widget base name, $widgets);
        unset($sidebars[$sidebar_id][$key]);
        update_option(`sidebar_widgets', $sidebars);
      }
    }
  }
}  

obviously you can start with locating the widget id in the widget's option and then loop over the sidebar.

Obviously it is just meta code and some small details might need to be adjusted, but pay attention that the unset here is probably done in a not very safe way.

Source Link
Mark Kaplun
  • 23.7k
  • 7
  • 43
  • 65

Widgets should only be manipulated by users, and in general they should not be deleted because there is no version control for them. Therefor I suggest you rethink first before reading further.

Widgets are located in two places in the DB, the sidebar_widgets option is an array of sidebars, indexed by the sidebar id. Each sidebar is represented by an array of widget identifiers.

The individual widget settings are located at an option called widget_{widget name} which is an array indexed by the widget id of the widget.

So to removed some widgets of a specific type you need to do something like

$sidebars = get_option(`sidebar_widgets');
$widgets = get_option('widget'.your widget base name);
foreach ($sidebars as $sidebar_id => $sidebar) {
  foreach ($sidebar as $key=>$widget_id) {
    if ($widget_id match the name pattern of the widget type) {
      $widget = $widgets[$widget_id];
      if ($widget needs to be deleted)
        unset($widgets[$widget_id]);
        update_option('widget'.your widget base name, $widgets);
        unset($sidebars[$sidebar_id][$key]);
        update_option(`sidebar_widgets', $sidebars);
      }
    }
  }
}  

obviously you can start with locating the widget id in the widget's option and then loop over the sidebar.

Obviously it is just meta code and some small details might need to be adjusted, but pay attention that the unset here is probably done in a not very safe way.