Support » Plugin: Widget Customizer » Initialise JavaScript on add / save widget?
Initialise JavaScript on add / save widget?
-
I was wondering if you know of / provide any hooks I could use to reinitialise some javascript on the widgets on save, or on adding a widget.
For example – I have some jQuery enhancing a widget’s select element, but when I save it or add a new widget the enhanced select element is wiped and reverts to the ordinary html one. I need some kind of hook so I can reinitialise the jQuery on save and add.
Thanks
-
There are hooks, yes, but they actually were removed in the version of Widget Customizer merged into core for the upcoming 3.9 release. I recommend removing
add_theme_support( 'widget-customizer' )
from your theme since it is not included in the merge. Adding widgets (for now) will require a refresh, which should invalidate your issue.For more info, see http://wptavern.com/widget-customizer-officially-merged-into-core-for-wordpress-3-9#comment-52863
Sorry, I realized that you’re talking about widget controls added to the panel instead of the widget being rendered into the preview, so my comment above is invalid.
I see your question has also been posted at http://wordpress.stackexchange.com/questions/138355/using-javascript-on-the-new-widget-preview-customizer-page
@danbrownweb Would you be able to be able to share the full PHP and JS code for the widget you’re developing? You can email it to my first name at x-team.com
Thanks very much, I’ve sent an email over to you now
I’ve taken the opportunity to fix the underlying problem in core where there is no widget-form-update event that fires when a widget’s form gets replaced. Please see https://core.trac.wordpress.org/ticket/27491
For how a widget would utilize the new event on both the widgets admin page and in the customizer, see https://gist.github.com/westonruter/9676069#file-customize-widgets-dynamic-fields-test-js
Thanks Weston! That’s really good of you to sort out the issue in core as well, it looks like it will help a lot. Do you know when I’ll be able to test it?
- The topic ‘Initialise JavaScript on add / save widget?’ is closed to new replies.