How do I add a simple jQuery script to WordPress?

WordPress is an open source CMS, used to develop dynamic websites. Let’s learn how to add jQuery script to WordPress. In the WordPress Theme folder, create a folder "js", and add a file in that. That file will be your Query file with extension “.js”. Add your jQuery script to that file. We have named it new.js,

Here’s your jQuery script:

jQuery(document).ready(function($) {
  $('#nav a').last().addClass('last');

Open your theme's functions.php file. Use the wp_enqueue_script() function so that you can add your script. 

Here’s the code:

add_action( 'wp_enqueue_scripts', 'add_my_script' );

function add_my_script() {
   // name your script to attach other scripts and de-register, etc.
   wp_enqueue_script (
     get_template_directory_uri() . '/js/add_my_script.js',
     array('jquery') // this array lists the scripts upon which your script depends

Assuming that your theme has wp_head and wp_footer in the right places, this will work. 

Updated on: 04-Dec-2019


Kickstart Your Career

Get certified by completing the course

Get Started