JQuery: Code Snippets for WordPress

Manuel campos

JQuery is a JavaScript library that’s very popular with WordPress developers for creating unique, dynamic, and engaging websites.

JQuery is also a solid choice for customizing themes and developing features for plugins. 

These are some helpful code snippets if you use WordPress

JQuery: Code Snippets for WordPress

If JQuery isn’t loading and you really need it, you can add this code snippet to your functions.php file

wp_enqueue_script("jquery");

If you need to remove JQuery Migrate, use this code snippet

function remove_jquery_migrate( $scripts ) {
	
	if ( ! is_admin() && isset( $scripts->registered['jquery'] ) ) {
			
		$script = $scripts->registered['jquery'];
		
		if ( $script->deps ) { 
			$script->deps = array_diff( $script->deps, array( 'jquery-migrate' ) );
		}
	}
}
add_action( 'wp_default_scripts', 'remove_jquery_migrate' );

If you need to remove JQuery, you can use this code snippet

/** * Completely Remove jQuery From WordPress */
function my_init() {
    if (!is_admin()) {
        wp_deregister_script('jquery');
        wp_register_script('jquery', false);
    }
}
add_action('init', 'my_init');

This is the script that you should add to your site header so it can load JQuery from Google CDN

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

More Code Snippets

These are other code snippets that you might find useful

  1. How to Load Scripts only on Mobile Devices
  2. Insert Adsense Ads Without a Plugin
  3. How to Remove WordPress Admin Bar Items
  4. How to Disable WordPress Post Tags
Wanna buy me a Coffee?
JM
About me
I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.
Linktree
Care for Site Speed?
I think you can’t go wrong with Cloudways, CloudFlare, and Perfmatters.
Perfmatters Cloudways

WPSurfer.com

Resources to host, secure, speed up, rank, and monetize your WordPress sites.
Plugins
Services
Connect
LinkedIn
Facebook
LinkTree