开发者

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

开发者 https://www.devze.com 2023-01-23 12:47 出处:网络
I have a problem getting some s开发者_StackOverflow社区cript 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 dr

I have a problem getting some s开发者_StackOverflow社区cript 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.

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.


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
});


I got the tumbleweed badge for this one!

Anyway, the answer is:

$( ".selector" ).bind( "sortstop", function(event, ui) {
  ... 
});
0

精彩评论

暂无评论...
验证码 换一张
取 消