1

I have a small plugin, which registers a custom widget and now I am wondering how to remove all traces of this widget when the plugin is removed through the administration.

If I look in the database after removal of the plugin, I can still see traces of the widget in the wp_options table:

+-----------+----------------------------+--------------------------------+----------+
| option_id | option_name                | option_value                   | autoload |
+-----------+----------------------------+--------------------------------+----------+
|       158 | widget_my_plugin_widget | a:1:{s:12:"_multiwidget";i:1;} | yes      |
+-----------+----------------------------+--------------------------------+----------+

+-----------+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------+
| option_id | option_name      | option_value                                                                                                                                                                                                                                                                                                 | autoload |
+-----------+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------+
|       102 | sidebars_widgets | a:5:{s:19:"wp_inactive_widgets";a:0:{}s:9:"sidebar-1";a:7:{i:0;s:8:"search-2";i:1;s:14:"recent-posts-2";i:2;s:17:"recent-comments-2";i:3;s:10:"archives-2";i:4;s:12:"categories-2";i:5;s:6:"meta-2";i:6;s:21:"my_plugin_widget-2";}s:9:"sidebar-2";a:0:{}s:9:"sidebar-3";a:0:{}s:13:"array_version";i:3;} | yes      |
+-----------+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------+

Is there some standard for removing all traces of a custom widget when the user removes a plugin?

I tried doing something like this, which seems to work, but was wondering if there is some better way or if it is needed at all:

// Delete any sidebar widgets.
$sidebars = get_option('sidebars_widgets');
foreach ($sidebars as $sidebar_id => $sidebar) {
  if (is_array($sidebar)) {
    foreach ($sidebar as $key => $widget_id) {
      if ($widget_id && strstr($widget_id, 'widget_my_plugin_widget')) {
        unset($sidebars[$sidebar_id][$key]);
      }
    }
  }
}

Your Answer

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

Browse other questions tagged or ask your own question.