Adding jQuery to a WordPress Website

This tutorial walks you through step by step how to add jQuery to Wordpress both directly and via the use of plugins.

In this tutorial we’re going to discuss the different methods for adding jQuery to WordPress. You may have heard of jQuery before or not but it’s a Javascript library which in basic terms simplifies a number of things that you could do with Javascript making some actions that you would ordinarily conduct with Javascript much easier.

Whether you’re looking to add a snippet of jQuery for one function to your site or you’re already a seasoned jQuery developer on another platform there are a few really easy ways to add it to your site.

Firstly jQuery is already integrated into WordPress meaning that you don’t need to worry about installing jQuery or linking to the CDN. You simply need to use the below call:

wp_enqueue_script(“jquery”);

One important thing to note is that WordPress uses a compatibility mode for jQuery by default. This replaces the $ sign with jQuery to avoid any conflicts with other languages such as Javascript. This is considered by many plugin developers and is why you see it in this format in the code.

The first option for adding jQuery code to WordPress is to create a Javascript file within your sites file structure. To do this, create a JS folder within your theme if you don’t have one already. In that folder add something like jquery-script.js you won’t need to add script tags as it’s already a Javascript file.

The Code:
jQuery( “a” ).click(function( event ) {

   event.preventDefault();

   jQuery( this ).show( “function” );

});

Then you simply need to enqueue the use of the script as mentioned above, that might look something like this:

function jquery_scripts() { 

 

wp_register_script( ‘jqueryscript’, get_template_directory_uri() . ‘/js/jquery-script.js’, array(‘jquery’),’1.0.0′,true );

 

 wp_enqueue_script( ‘jqueryscript’ ); 

 

} 

 

add_action(‘wp_enqueue_scripts’, ‘jquery_scripts’)

The second option for adding jQuery to WordPress is to use one of the many plugins available within the plugin repository.

For Example:

Simple Custom CSS and JS:

https://en-gb.wordpress.org/plugins/custom-css-js/ This plugin will allow you to add jQuery under the Javascript type.

Using a plugin is beneficial because generally they can handle standard jQuery meaning that the $ identifier can be used.

The third option for adding jQuery to WordPress is to add it to your themes built-in Javascript code additions section. Many themes will have at least CSS addition functionality which is usually bundled with the ability to add custom Javascript however not all themes do include this. It will also depend on how your theme handles jQuery as to whether you have to write functions using jQuery or $ so it’s worth reading the documentation on this.

Further Reading:

Editing a CSS class name with jQuery

Adding Javascript to WordPress

Adding jQuery to WordPress can seem like quite a complicated task but it’s actually relatively simple as you get into it, whether you’re writing it directly to a theme, using a plugin or using built-in theme tools there are multiple ways to achieve what you need.  We’d appreciate a share or any comments if you found this useful, thanks!

Tech Typed
Tech Typed
Articles: 186

Leave a Reply

Your email address will not be published. Required fields are marked *