JQuery: Code Snippets for WordPress

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

//Remove JQuery migrate
 
function remove_jquery_migrate( $scripts ) {
   if ( ! is_admin() && isset( $scripts->registered['jquery'] ) ) {
        $script = $scripts->registered['jquery'];
   if ( $script->deps ) { 
// Check whether the script has any dependencies

        $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
JM

Sobre Jose manuel

I am José Manuel. I started a WordPress blog in 2011 as a hobby to use it a part of my teaching and I fell in love with blogging & WordPress.

I am writing about things I know and things that I am learning. I hope you find some of the things I write useful.