How should I add JavaScript to a child theme?

You can do different things: Enqueue the new JS file via plugin Enqueue the new JS file via functions.php Add your JS code into functions.php through wp_header or wp_footer For adding new JS files to a wordpress site you should enqueue these files with wp_enqueue_style() and/or wp_enqueue_script(). This is the proper and safest way. I … Read more

Gutenberg Modify core taxonomy panel element via wp.hooks.addFilter

Because your plugin handles both hierarchical and non-hierarchical taxonomies, then I think it might be a better option to copy the HierarchicalTermSelector component, and return the copy from the CustomizeTaxonomySelector function. And you can see how I did it here — Updated to use the JavaScript Build Setup which makes things like transpilation super easy. … Read more

Rest API authentication issue when called from fetch request in bundle.js

The problem is that you’re not using the correct name for the REST API nonce header — the correct name is X-WP-Nonce, but you used X-WP-Header: fetch(‘http://localhost/wptest2/?rest_route=/wp/v2/users/me’, { method : ‘get’, mode : ‘cors’, headers : { ‘Access-Control-Allow-Origin’ : ‘*’, ‘X-WP-Header’ : _wpnonce // here you used the wrong name } }) Reference from the … Read more

Using wp_ajax and wp_ajax_nopriv hooks

It depends on what exactly you want to do. If you want to show just a short message, use the same callback for both. But if you need completely different objects and load different additional files depending on the log-in status, use separate callbacks. Basic example, probably not a recommended implementation. 🙂 class Ajax_Controller { … Read more

Add confirmation popup on “Move to Trash”

You can try to admin enqueue this script, with jquery dependency: jQuery( function($) { $(‘.edit-php a.submitdelete, .post-php a.submitdelete’).click( function( event ) { if( ! confirm( ‘Are you sure you want to move the post to trash?’ ) ) { event.preventDefault(); } }); }); where we restrict the conformation on the a.submitdelete clicks to the .edit-php … Read more

How do I fire a snackbar notice in admin?

WordPress has some global actions you can use here. If you want to add your own notice in the lower corner (like the screenshot), you can do that like this: wp.data.dispatch(“core/notices”).createNotice( “success”, // Can be one of: success, info, warning, error. “This is my custom message.”, // Text string to display. { type: “snackbar”, isDismissible: … Read more

Javascript Helpers

WordPress installs with a number of libraries/classes/plugins/etc that you can enqueue and use. For base url specifically, or any vars in general you need to access from javascript, use the api php functions, then pass it to your javascript via wp_localize_script.