Skip to content
Home » How to Include jQuery in WordPress – Program to Use

How to Include jQuery in WordPress – Program to Use

In this blog post, we will discuss how to include the jQuery library in WordPress. jQuery is a popular JavaScript library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. By including jQuery in your WordPress website, you gain access to a wide range of built-in functions and effects that can enhance the user experience.

Code

wp_register_script( 'jQuery', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js', null, null, true );
wp_enqueue_script('jQuery');

Code Explanation

To include jQuery in WordPress, you can use the wp_register_script() and wp_enqueue_script() functions. The wp_register_script() function registers the jQuery script and assigns it a handle, in this case, 'jQuery'. It takes multiple arguments: the first one is the handle, the second one is the source URL for the jQuery script, and the remaining arguments are optional and specify dependencies, version, and whether to load the script in the footer or header.

Here is an example code snippet:

php
wp_register_script( 'jQuery', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js', null, null, true );
wp_enqueue_script('jQuery');

In the above code, we register the jQuery script with the handle 'jQuery' and provide the source URL from the cdnjs.cloudflare.com CDN. The script is set to load in the footer (true value). After registering the script, we use the wp_enqueue_script() function to enqueue it, making it available for use in our WordPress theme or plugin. By enqueuing the script, WordPress will automatically include it in the HTML document.

Conclusion

Including jQuery in your WordPress website can greatly enhance its functionality and interactivity. By following the simple steps outlined in this blog post, you can easily include jQuery using the `wp_register_script()` and `wp_enqueue_script()` functions. Now, you can leverage the power of jQuery to create dynamic and engaging user experiences on your WordPress site. Happy coding!

Also checkout the following codes.


How to Trim a String in JavaScript – Program to Limit Text Length
How to Dynamically Add Hidden Input Field in JavaScript