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

Glidejs with WordPress

It’s because you’re using an import statement in the script tag for single.php. You would only use the import statement in an ES module as shown in your screen capture. Because you’re enqueueing the script in functions.php, glide.js is available globally and you should remove the import statement from the script tag and glide will … Read more