WPSurfer.com

JQuery: Code Snippets for WordPress

Published on July 11, 2022 | Updated on September 24, 2022

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>

Manuel Campos

Manuel Campos

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.