How to override JavaScript files in child theme?

Child themes only override php files (like header.php) that are included with functions like get_template_part or get_header, etc. The correct way to add scripts to WordPress is with wp_enqueue_script. If your parent theme uses this, you can override the JS files by using wp_dequeue_script and enqueuing your own. Like so… <?php // hook in late … Read more

Where is the right place to register/enqueue scripts & styles

Why registering and queuing properly matters it should be in time – earlier than script/style is up for being output to page, otherwise it is too late; it should be conditional – otherwise you are loading stuff where you don’t need it and cause performance and functionality issues, for this you need WP environment loaded … Read more

When should I use wp_register_script() with wp_enqueue_script() vs just wp_enqueue_script()?

The wp_register_script() Codex page literally says: A safe way of registering javascripts in WordPress for later use with wp_enqueue_script(). This means, if you want to register your scripts, but not directly load them in your pages, you can register the files once, and then load them when you need them. For example: You have a … Read more

How do I Enqueue styles/scripts on Certain /wp-admin Pages?

add_menu_page and add_submenu_page both return the page’s “hook suffix”, which can be used to identify the page with certain hooks. As such, you can use that suffix in combination with the variable hooks admin_print_styles-{$hook_suffix} and admin_print_scripts-{$hook_suffix} to specifically target these pages. function my_menu() { $menu = add_menu_page( ‘Page 1’, ‘bar’, ‘something’, ‘else’, ‘foo’ ); $submenu … Read more

Enqueue Scripts / Styles when shortcode is present

I found an other way that works well for me: When initializing the plugin, do not enqueue your scripts and styles, but register them with wp_register_style and wp_register_script. Next you can load the script/style on demand. For example when you render a shortcode with wp_enqueue_style(“your_style”) and wp_enqueue_script(“your_script”). Here is an example plugin using this method … Read more