Update widget form after drag-and-drop “only specific plugin”

Update 27 July 2015: Added new test to make sure settings.data.search is defined to handle some non-standard sidebar implementations such as WPMU’s Custom Sidebars.


I ran into this problem with one of my plugins, and am deeply indebted to this article for pointing me in the right direction.

The key is that the third parameter you get in the callback includes the widget’s basename (derived from its class). I used a slightly different AJAX function than the example you linked to, but here’s what I came up with:

$(document).ajaxSuccess(function(e, xhr, settings) {

    // test to see if any instance of a particular widget is saved
    if( undefined != typeof settings.data.search && settings.data.search('action=save-widget') != -1 && settings.data.search('id_base={YOUR BASENAME GOES HERE}') != -1 ) {
        // do something
    }

    // test to see if any widget was added or re-ordered
    if(settings.data.search('action=widgets-order') != -1) {
        // do something
    }

}); 

A couple notes:

  1. Make sure to fill in “{YOUR BASENAME GOES HERE}” above.
  2. Note that you can’t see which particular widget was added or re-ordered. That’s not passed.
  3. If you’re unsure of your basename, just add console.log(settings); to the first line of the function and look at it in a JS browser console. You’ll see your basename buried in there.