1

On the WordPress admin widgets page there is a known issue with the widget form not being available the first time a widget is added to a widget area. This can be seen in this trac ticket here: https://core.trac.wordpress.org/ticket/14686.

One solution to this is to force a Save operation the first time the widget is added, like so:

jQuery(document).ready(function ($) {
    $(document).ajaxSuccess(function (event, XMLHttpRequest, ajaxOptions) {
        var request = {}, pairs = ajaxOptions.data.split('&'), i, split, widget;
        for (i in pairs) {
            split = pairs[i].split('=');
            request[decodeURIComponent(split[0])] = decodeURIComponent(split[1]);
        }
        if (request.action && (request.action === 'save-widget')) {
            widget = $('input.widget-id[value="' + request['widget-id'] + '"]').parents('.widget');
            if (!XMLHttpRequest.responseText) wpWidgets.save(widget, 0, 1, 0);
        }
    });
});

This isn't perfect but it works well and the widget instance form is available as soon as the widget has been added to a widget area.

The problem is that the above code only works on the WordPress admin widgets page. It doesn't work on the Customizer, where you can now add widgets to widget areas in the latest version of WordPress.

I need a way to also access the widget instance form when adding widgets to a widget area via the Customizer.

There are new JavaScript events added to the latest version such as 'widget-added' and ‘widget-updated’ which could possibly be used somehow to save the widget instance similar to the solution used on the WordPress admin widgets page but I haven't found a way to do that yet.

The syntax for using these new events is:

$( document ).on( "widget-added", function(event, widget) {
    console.log("widget-added");
});

$( document ).on( "widget-updated", function() {
    console.log("widget-updated");
});
  • can you give an example on how to test? – Reigel Mar 20 '15 at 9:43
  • The trac ticket I linked to has a test widget to demonstrate the issue. – dgwyer Mar 20 '15 at 13:57
  • A solution to this problem can found here: reigelgallarde.me/programming/…. – dgwyer Mar 23 '15 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.