Skip to content

Instantly share code, notes, and snippets.

@omniacode
Created November 10, 2022 15:46
Show Gist options
  • Save omniacode/96096021d7d6cb6f73913016697b413c to your computer and use it in GitHub Desktop.
Save omniacode/96096021d7d6cb6f73913016697b413c to your computer and use it in GitHub Desktop.
Conditionally Enqueue Scripts on Specific Page If It Is Not Already Enqueued
add_action( 'wp_enqueue_scripts', 'enqueue_my_custom_script' );
function enqueue_my_custom_script() {
if ( 'page' === get_post_type() && is_page('ID') ) {
if ( wp_script_is( 'file.js', 'enqueued' ) ) {
return;
} else {
wp_register_script( 'file name', get_stylesheet_directory_uri() . '/js/file.js' );
wp_enqueue_script( 'file name' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment