WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to enque scripts only on certain pages (2 posts)

  1. scribu
    Member
    Posted 6 years ago #

    Hello! I use wp_enque_script to add several scripts to the page header. I would like to be able to add them only on certain pages, like the home page. The problem is that conditional tags such as is_home() don't seem to work.

    This is how I'm currently including the scripts:

    add_action('init', 'js_loader');
    
    function js_loader(){
       wp_enque_script('script_name', 'script_src', array('dependencies'));
    }

    Conditional tags added inside js_loader() or wrapped around add_action() do not seem to work.

    Any ideas?

  2. scribu
    Member
    Posted 6 years ago #

    The answer was to use 'template_redirect' instead of 'init'.

Topic Closed

This topic has been closed to new replies.

About this Topic