JavaScript
Registering and Enqueueing
// Use the wp_enqueue_scripts function for registering and enqueueing scripts on the front end.
add_action( 'wp_enqueue_scripts', 'register_and_enqueue_a_script' );
function register_and_enqueue_a_script() {
// Register a script with a handle of `my-script`
// + that lives inside the theme folder,
// + which has a dependency on jQuery,
// + where the UNIX timestamp of the last file change gets used as version number
// to prevent hardcore caching in browsers - helps with updates and during dev
// + which gets loaded in the footer
wp_register_script(
'my-script',
get_template_directory_uri().'/js/functions.js',
array( 'jquery' ),
filemtime( get_template_directory().'/js/functions.js' ),
true
);
// Enqueue the script.
wp_enqueue_script( 'my-script' );
}Localizing
Deregister / Dequeueing
AJAX
The JavaScript side of WP AJAX
AJAX on click
Multiple callbacks for a single AJAX request
Chaining callbacks
Last updated