WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm pulling in scripts.php through functions.php. this is in scripts.php but for some reason, wordpress isn't recognizing is_home(). i've tried resetting the query, but to no avail. Am I hooking into the right function?

if(is_home()){

function my_scripts_method2() {
    wp_register_script('cycle', get_template_directory_uri() . '/js/cycle.js', array('jquery'));
    wp_enqueue_script('cycle');
}
add_action('wp_enqueue_scripts', 'my_scripts_method2');

function my_scripts_method() {
    wp_register_script('homepage', get_template_directory_uri() . '/js/homepage.js', 'cycle');
    wp_enqueue_script('homepage');
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
}
share|improve this question
up vote 9 down vote accepted

At the time functions.php is included during bootup, WordPress has no idea on the contents of the query, and doesn't know the nature of the page. is_home will return false.

Wrap the code in a function and have it triggered by the wp hook, which comes after the global query object has been hydrated with data.

add_action( 'wp', 'wpse47305_check_home' );
function wpse47305_check_home() {
    if ( is_home() )
        add_action( 'wp_enqueue_scripts', 'my_scripts' );
}

function my_scripts() {
    ...
}

The wp_enqueue_scripts action runs after wp.

http://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_a_Typical_Request

share|improve this answer

try with $_SERVER[REQUEST_URI] . It works for me ;) In my case I have this script for charging the enqueue scripts only on the front:

if ($_SERVER[REQUEST_URI]=='/'){
    add_action('wp_enqueue_scripts', '***********');
}

echo the $_SERVER[REQUEST_URI] and you'll see his value for each page you are so you can work with it ;)

share|improve this answer
    
So you assume that $_SERVER['REQUEST_URI'] will always equal / on is_home()? Aside from missing quotes around that array key, you will have problems running this on the command line and maybe with cron jobs. Also a server could be set up completely different, or you could define /home as your home directory without stripping it. In short: This will not work. – kaiser Feb 27 at 12:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.