How can I hook into the jquery ui drop event when moving widgets around in WordPress?

I have a problem getting some script to run in a wordpress widget when it is needed. Ideally I would like to be able to hook into the javascript event fired when a widget is dropped into a sidebar, or when the widget is opened up for the first time to run some setup javascript.

Unfortunately script tags contained in the widget form itself are stripped out unless the widget is already in a sidebar, so the problem exists when a new widget is added dynamically and hasn’t been saved yet.

Read More

I’ve got a nasty hacky solution using a jquery .live() event binding on mouseover of the widget to fire off my code but I’d like to think there’s a better way.

Related posts

Leave a Reply

2 comments

  1. Just to clarify, as of WordPress 3.2.1, the exact selector you would use would be “div.widgets-sortables”, so the code would look like:

    jQuery('div.widgets-sortables').bind('sortstop',function(event,ui){
        // run js after widget dropped into sidebar area
    });