How to fire a function before and after automatically in a jquery plugin?

WordPress Theme Development: Is there a way to trigger a JavaScript function when a particular widget is drag-and-dropped in widgets.php of the admin area?

  • This kind of works but it applies to all widgets. jQuery('div.widgets-sortables').bind('sortstop',function(event,ui){ // run js after widget dropped into sidebar area }); ( http://stackoverflow.com/questions/4087677/how-can-i-hook-into-the-jquery-ui-drop-event-when-moving-widgets-around-in-wordp ) I'm wondering if it could be only applied to the widget that I create for my plugin or theme.

  • Answer:

    Hi, just replace 'your_widget' with name of your widget in the code below (two places).  'sortreceive' event is only called when widget is added to sidebar, while 'sortstop' gets called whenever you move the widget around inside sidebar or remove it. 'sortstop' is also called when widget is added, but for some reason ui.input is not set properly, so i used 'sortreceive' to cover that. jQuery('div.widgets-sortables').bind('sortstop',function(event,ui){ var id = jQuery(ui.item).attr('id'); if (id) { var widget_type = id.match(/widget-[0-9]+_(.+)-[0-9]+/i)[1]; if (widget_type == 'your_widget') { // do stuff; } } }) jQuery('div.widgets-sortables').bind('sortreceive',function(event,ui){ var id = jQuery(ui.item).attr('id'); var widget_type = id.match(/widget-[0-9]+_(.+)-__i__/i)[1]; if (widget_type == 'your_widget') { // do stuff; } })

Arūnas Liuiza at Quora Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.