WordPress Script Loading/Unloading — wp_deregister_script(‘jquery’)

WordPress has essentially two groups of methods to handle scripts, both of which should be used:

  • wp_register_script Registers a script in WordPress. It does not get called, it is just available for WordPress, if it is needed.
  • wp_deregister_script is the exact opposite. It deletes the definitions made in wp_register_script, the script is no longer available as a dependency or for enqueueing.

The registered scripts do not output anything in code. They just define the script programmatically so WP can react on dependencies and enqueues.

  • wp_enqueue_script actually defines and queues a script for outputting in the header/footer of the HTML page.
  • wp_dequeue_scriptdoes the exact opposite and dequeues a script for outputting.

The actual printing happens on the action ‘wp_head’ or ‘wp_footer’, depending on the register details.

An example:

wp_deregister_script('jquery');
wp_register_script('jquery', ("https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"), false, '1.9.1', true);

wp_enqueue_script( 'script-1', get_template_directory_uri() . '/js/example1.js', array('jquery'), '1.0.0', false );
wp_enqueue_script( 'script-2', get_template_directory_uri() . '/js/example2.js', array('jquery'), '1.0.0', true );
wp_enqueue_script( 'script-3', get_template_directory_uri() . '/js/example3.js', array('jquery'), '1.0.0', true );
wp_enqueue_script( 'script-4', get_template_directory_uri() . '/js/example4.js', array('jquery'), '1.0.0', true );
wp_enqueue_script( 'script-5', get_template_directory_uri() . '/js/example5.js', array('jquery'), '1.0.0', true );

In this example I deregister jQuery. It is no longer avalable and I would get the same error as you, if I would not register it again. (This time from the Google CDN in the hope, that it speeds up my page.)

But upon line three jQuery will not be printed. It will not appear anywhere. But from line four I call a bunch of scripts, which all have jquery as a dependency. The first one resides in the header, all the others in the footer. Due to the dependencies, jQuery gets automatically enqueued in the header, because it is first needed there.

So, with the scripts system of WordPress, which is quite solid, there is no need to handle interferences or previous requests. Registered scripts are called when needed once, and only when needed.

In your example you deregister the script in line 1 of the function and call it again in line 2. But due to deregistering WordPress has forgotten all of the details as URL, Version, dependencies, and so forth. The correct function in you logic would have been wp_dequeue_script, but even that isn’t needed. WordPress evaluates the need by either looking at the enqueued scripts, or the dependencies.

Edit:

how does wordpress know how to stop the mess left by 8 different plugins all trying to add different versions of jquery?

The last instance of registering jquery before the wp_print_scripts action is the one that get’s printed. That is essentially what the priority system in actions/filters is for.