How to add jQuery script into WordPress

How to add jQuery script into WordPress

You can add jQuery scripts to your WordPress site by utilizing some built-in WordPress functions which direct the CMS to load the script after jQuery has been loaded so that the jQuery alias $ is not undefined.

1.) Upload your script to: wp-content/themes/[theme-name]/assets/js/
2.) Open: functions.php
3.) Register script using wp_register_script() function
4.) Add action using add_action() function

<?php
/****************************************************************
 * Theme functions and definitions.
 ****************************************************************/
if ( ! defined( 'ABSPATH' ) ) {
    exit; 
}

function nthEverything() { 
	wp_register_script(
		'nthEverything', 
		get_template_directory_uri() . '/assets/js/jquery.nthEverything.js', 
		array('jquery')); 
}
add_action('wp_enqueue_scripts', 'nthEverything');

Additional Links
https://wordpress.stackexchange.com/questions/48530/how-do-i-make-script-load-after-jquery
https://www.codecanal.com/add-simple-jquery-script-wordpress/



Leave a Reply

Your email address will not be published.

*
*
*