So I have some widgets that are using some custom jquery on the backend. I can get the widget forms to call the jquery files but only AFTER I’ve saved the widget and reloaded the page.
Is there anyway to use that jquery as soon as I’ve dragged it from the “Available Widgets” area to the sidebar area?
So in my widget code, in the form function, I have this:
function form( $instance ) {
wp_enqueue_media();
wp_enqueue_script('kjd_widgets',plugins_url('lib/kjd_widgets.js',__FILE__),null);
so that works but only after ive reloaded the page. How can I get the JS to load when the widget is activated and? And would I have to add something to my jQuery to bind the new widget form to the jQuery?
I know this issue from this Q&A: Update widget form after drag-and-drop (WP save bug).
I was answering a Stack Overflow question and faced the same problem. We have to add an
AjaxComplete
action and fire our function whenXMLHttpRequest.responseText
is true. I’m not sure about the inner workings but we only have to modify one line in the following code (by onetrickpony):Demo plugin
And onetrickpony’s code adapted to our widget (
/wp-content/our-plugin/test-me.js
):