• Hello, I am authoring a WP Widget plugin, and have run into a problem. My Widget’s Admin Form is quite involved, and uses an external javascript file to manipulate fields, collapse sections, etc. This external javascript is enqueued into the Widget Admin Panel like this:

    function enqueue_admin_scripts() {
      wp_register_script('mp3-admin-script', plugins_url('assets/js/wp-mp3-admin.js' , __FILE__ ), array( 'jquery' ));
      wp_enqueue_script('mp3-admin-script');
    }
    add_action('admin_enqueue_scripts','enqueue_admin_scripts');

    This works fine initially, but when you make a change to the Widget’s Admin Form and click the “Save” button, the page is reloaded but WITHOUT any of the JavaScript.

    If I refresh the page, the javascript is loaded again and resumes working, but each time the Widgets’ changes are saved, the javascript stops functioning.

    Any ideas why ‘admin_enqueue_scripts’ doesn’t get executed after a widget is “Saved”?

    Thanks for looking!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The entire Widget Admin Panel reloads? That sounds odd already. I’m pretty sure all the default widgets save via AJAX and never trigger a reload, only the related HTML is updated via jQuery.

    My guess is something is causing a JS error when you save your widget form. It may not even be your widget, perhaps there’s a name collision with another plugin or your theme. Have you checked your browser console for JS errors after a save? That would explain why JS no longer works.

Viewing 1 replies (of 1 total)

The topic ‘Widget Admin Forms and Javascript enqueuing’ is closed to new replies.