Skip to content

Instantly share code, notes, and snippets.

@omniacode
Last active November 10, 2022 15:42
Show Gist options
  • Save omniacode/5c80432ae27bd261fdecf5803c3439e8 to your computer and use it in GitHub Desktop.
Save omniacode/5c80432ae27bd261fdecf5803c3439e8 to your computer and use it in GitHub Desktop.
Conditionally Enqueue JS per Post Type
add_action( 'wp_enqueue_scripts', 'enqueue_cpt_script' );
function enqueue_cpt_script() {
if ( 'cpt-type' === get_post_type() ) {
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