Skip to main content
15 votes
Accepted

Cannot deregister a script using wp_deregister_script

When attempting to dequeue a script, we need to hook in after the script is enqueued, but before it's printed. In this case, the Disqus plugin is using the wp_footer hook at a priority of 10 to ...
Nathan Johnson's user avatar
12 votes

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

I have gone through some articles and come to the following conclusion. I think it helps. Registering any scripts using wp_register_script() is just registering; not loading it. The registered script ...
Mohammad Shohel Rana's user avatar
11 votes

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

IMHO the main advantage of using wp_register_script before wp_enqueue_scripts is illustrated in the following paragraph from Codex: Scripts that have been pre-registered using wp_register_script() ...
Franco's user avatar
  • 211
9 votes
Accepted

How to dequeue / deregister any theme styles and scripts

The tricky thing is knowing whether or not a particular script or style was enqueued by the theme. Themes and plugins both use the same hooks and functions, so they're not explicitly labelled in any ...
Jacob Peattie's user avatar
8 votes
Accepted

How can I load a javascript file that is type="module" the Wordpress way?

Here is the final code I now use to add type="module" to my scripts. wp_register_script('my-script', get_theme_file_uri('/js/script.js'), ['axios'], '1.0'); wp_enqueue_script('my-script'); ...
Robbiegod's user avatar
  • 543
7 votes
Accepted

Enqueue script only when shortcode is used, with WP Plugin Boilerplate

Why it's not working: You need to use the wp_enqueue_scripts action hook. You've used wp_register_script as an action hook, however, there is no action hook named wp_register_script in WordPress core....
Fayaz's user avatar
  • 8,937
6 votes

Enqueue Scripts / Styles when shortcode is present

WordPress has a built-in function to do something based on a specific Shortcode presenting status. The function name is has_shortcode(). You can use the following code to enqueue your style and ...
Arif Rahman's user avatar
6 votes
Accepted

is_page() function doesnt working

The init hook is too early — WordPress hasn't yet identified the queried object (post, category, etc.); hence the is_page() doesn't work there: add_action('init', 'register_script'); So for ...
Sally CJ's user avatar
  • 39.3k
5 votes

How to properly dequeue scripts and styles in child theme?

This is not the proper way. WordPress said not to use both wp_print_scripts, and wp_print_styles since WordPress 3.3. Either you are enqueueing or dequeueing, the proper way is: function ...
Morshed Alam Sumon's user avatar
4 votes

Enqueue Scripts / Styles when shortcode is present

I do so: class My_Shortcode { function __construct() { do_action('my_start_shortcode'); // call .... and catch hook in other functions (or other plugins): function ...
Vova Druzhaev's user avatar
4 votes
Accepted

Adding jQuery dependency prevents my script from enqueuing/loading

The handle for the jQuery dependency is actually, jquery (all lowercase and case sensitivity matters). The complete list of JavaScript handles are documented here in the Theme Developer Handbook.
Dave Romsey's user avatar
  • 17.7k
4 votes

Adding JS in header when using wp_enqueue_script in a shortcode

Page is created and output in the following order: header, content, footer. Shorcode is executed during the construction of the page content, when header is already output on the page. So, it is ...
KAGG Design's user avatar
  • 1,842
4 votes
Accepted

Registering a script dilemma→ wp_register_script

Simply pass in null or the empty equivalent. For example, if you don't depend on anything, say so. It's expecting an array of the dependencies, so pass an empty array array() Or pass in null
Tom J Nowell's user avatar
  • 59.6k
4 votes
Accepted

wp_register_script multiple identifiers?

To have the JavaScript Libraries not to load since you already created a bundle of them, do the following: Asumming the following, usual enqueue: function the_js() { wp_enqueue_script('bundle_js'...
David Lee's user avatar
  • 3,891
4 votes
Accepted

Enqueuing script is adding extra text for google maps

When you call google, the key variable name is missing. wp_register_script( 'googlemap', 'https://maps.googleapis.com/maps/api/js?123123123123' , '', '', false ); Should be : wp_register_script( '...
shanebp's user avatar
  • 5,063
3 votes
Accepted

Best spot for wp_register_script() and wp_register_style()

Scripts and styles can be registered on the wp_loaded hook and then later enqueued using wp_enqueue_scripts. Once the scripts and styles have been registered, they can be enqueued later using just ...
Dave Romsey's user avatar
  • 17.7k
3 votes
Accepted

WP .js script file not loading

According to codex, get_stylesheet_directory_uri() does not including a trailing slash. So, you might want to use it in the following way: function wpb_adding_scripts() { wp_register_script('...
Johansson's user avatar
  • 15.3k
3 votes

How to cancel `wp_print_scripts`?

There is a way, but it's not recommended since there might be some other inline scripts attached to the enqueued scripts. You can hook to the wp_enqueue_scripts action and empty the global $wp_scripts ...
Johansson's user avatar
  • 15.3k
3 votes

How to properly dequeue scripts and styles in child theme?

For Dequeue Scripts, don't add the last "-js" or "-css" on ID part. I was using WordPress 5.8. It works for me. ABOVE CODE: //Dequeue JavaScripts function ...
Chandra Shekhar Pandey's user avatar
3 votes

Enqueue Scripts / Styles when shortcode is present

As someone mentioned using 'has_shortcode' will do it. Here's the example I found very helpful (Source: Wordpress.org) function wpdocs_shortcode_scripts() { global $post; if ( is_a( $post, '...
Simon's user avatar
  • 145
3 votes
Accepted

Where does WordPress register default scripts like jQuery?

WordPress registers jQuery in the wp_default_scripts() function, which is found in wp-includes/script-loader.php. // jQuery $scripts->add( 'jquery', false, array( 'jquery-core', 'jquery-migrate' ),...
RRikesh's user avatar
  • 5,635
3 votes

Tell WP register script to load a script only when the entire page is loaded

wp_register_script() is for telling WordPress about script files, and is a PHP function. It has no control over how the script executes, only where the <script></script> tag is output and ...
Jacob Peattie's user avatar
3 votes
Accepted

Wordpress error when replacing local jQuery by externally-hosted

The error message describes your problem quite clearly: Notice: wp_deregister_script was called incorrectly. Do not deregister the jquery script in the administration area. To target the ...
Jacob Peattie's user avatar
2 votes
Accepted

How to make a customized user registration form using the built in wp-register() template tag

The Codex has an article that outlines how to do this: https://codex.wordpress.org/Customizing_the_Registration_Form It even provides an example: //1. Add a new form element... add_action( '...
darrinb's user avatar
  • 1,226
2 votes

Enqueue Scripts / Styles when shortcode is present

We can indeed conditionally load CSS and JS files via shortcode without using any overly elaborate functions: First, let's assume we registered all of our CSS/JS files previously (perhaps when ...
Christian Žagarskas's user avatar
2 votes
Accepted

wp_enqueue_scripts does not work

If do_action works, then the problem should be you are not calling wp_head() and wp_footer() in your header.php and footer.php respectively.
Aniruddha Gawade's user avatar
2 votes
Accepted

Scripts not loading through function Method in Wordpress Theme

There are several fundamental problems with what you're trying to do that prevent you reaching your goal The Problems Naming You register 2 scripts, but you name them both 'custom'. How is ...
Tom J Nowell's user avatar
  • 59.6k
2 votes

Adding JS in header when using wp_enqueue_script in a shortcode

While it is a pretty "dirty" fix, you can achieve the wanted effect like this: function wpdocs_theme_name_scripts() { wp_register_script('googlesearch', 'https://maps.googleapis.com/maps/api/js')...
HU is Sebastian's user avatar
2 votes
Accepted

Register and enqueue style.css custom theme

function.php should be functions.php.
janh's user avatar
  • 2,738
2 votes
Accepted

Can't get JS code to work with shortcode

You need to return the Shortcode generated string, not echo, like this: function gg_product_front_end() { wp_register_script( 'gg_loadProducts_frontEnd', plugins_url( 'js/front_end.js', __FILE__ )...
Fayaz's user avatar
  • 8,937

Only top scored, non community-wiki answers of a minimum length are eligible