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

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.

Javascript not included

You’re mixing up the singular wp_enqueue_script, which adds a script, with the plural wp_enqueue_scripts which is an action and a function that triggers that action. I think you want function theme_scripts() { wp_enqueue_script( ‘custom-script’, get_template_directory_uri() . ‘/js/menu-fix.js’, array(‘jquery’), ‘1.0’, true); } add_action(‘wp_enqueue_scripts’, ‘theme_scripts’); i.e. register against the enqueue_scripts-plural action that calls enqueue_script-singular.