Customizer JS API

1) Maybe bind to the api.ready state which may fix having to call your section twice (function($, api){ api.bind( ‘ready’, function() {… } })(jQuery); I saw a note in trac that said “Note that the APIs for dynamically-added controls, and APIs for JS-templated custom Sections and Panels are not yet available as of WordPress 4.2. … Read more

How to intercept already localized scripts

wp_localize_script() calls the method localize() on the global variable $wp_scripts. We can set this variable to an instance of a child class of WP_Scripts: class Filterable_Scripts extends WP_Scripts { function localize( $handle, $object_name, $l10n ) { $l10n = apply_filters( ‘script_l10n’, $l10n, $handle, $object_name ); return parent::localize($handle, $object_name, $l10n); } } add_action( ‘wp_loaded’, function() { $GLOBALS[‘wp_scripts’] … Read more

Nonce retrieved from the REST API is invalid and different from nonce generated in wp_localize_script

Take a closer look at the function rest_cookie_check_errors(). When you get the nonce via /wp-json/nonce/v1/get, you’re not sending a nonce in the first place. So this function nullifies your authentication, with this code: if ( null === $nonce ) { // No nonce at all, so act as if it’s an unauthenticated request. wp_set_current_user( 0 … Read more

Trigger Javascript on Gutenberg (Block Editor) Save

Not sure if there is a better way, but I am listening to subscribe rather than adding an event listener to the button: wp.data.subscribe(function () { var isSavingPost = wp.data.select(‘core/editor’).isSavingPost(); var isAutosavingPost = wp.data.select(‘core/editor’).isAutosavingPost(); if (isSavingPost && !isAutosavingPost) { // Here goes your AJAX code …… } }) Official docs of the Post Editor data: … Read more

Can WordPress be made to support websockets?

WebSockets use the websockets protocol: WS:/example.com/yourscript.js and open a synchronous connection – meaning the connection is held open and dedicated to the browser. httpd servers, like apache2 (used by most shared hosting providers) use the http protocol: http://example.com/yourscript.js and open an asynchronous connection – meaning that no connection is held open between the server and … Read more

wp_add_inline_script without dependency

wp_add_inline_style() – without dependency The wp_add_inline_style() can be used without a source file dependency. Here’s an example from @Flix: wp_register_style( ‘dummy-handle’, false ); wp_enqueue_style( ‘dummy-handle’ ); wp_add_inline_style( ‘dummy-handle’, ‘* { color: red; }’ ); where we would hook this into the wp_enqueue_scripts action. wp_add_inline_script() – without dependency According to ticket #43565, similar will be supported … Read more