Enqueue a script for specific pages

How do I enqueue scripts based on specific pages.

  • Dimitris Kalliris
    • Support Team Lead

    Hello there michael,

    hope you’re doing well! :slight_smile:

    There are some conditional tags that can be used for checking a specific page:

    – conditional tags: https://codex.wordpress.org/Conditional_Tags

    – check a specific page: https://developer.wordpress.org/reference/functions/is_page/

    So what you’re looking at is something like the following:

    <?php
    function my_custom_scripts() {
    wp_register_script( 'first', get_template_directory_uri() . 'js/first.js' );
    // Conditional tags: https://codex.wordpress.org/Conditional_Tags
    // Check a specific page: https://developer.wordpress.org/reference/functions/is_page/
    if ( is_page('Contact') ) {
    // How to enqueue scripts/styles
    // https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
    wp_enqueue_script('first');
    }
    }
    add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );

    Thank you,

    Dimitris