views:

58

answers:

2

Hi, can someone tell me why this code doesn't work? The is_page() is not in effect.

function tao_scripts() {
if (!is_admin()) {


    wp_deregister_script('jquery');
    wp_register_script('jquery', get_template_directory_uri() . '/js/jquery-1.4.2.min.js', false, '1.4.2');
    wp_enqueue_script('jquery');

    if ( is_page() ) {
        wp_register_script('jquery-validate', get_template_directory_uri() . '/js/jquery.validate.min.js', 'jquery', '1.7');
        wp_enqueue_script('jquery-validate');
    }       
  }
}
add_action('init', 'tao_scripts');

The code is in a php file which is included i my functions.php.

I have tried all I could find/think of. Without the is_page() conditional it works. I have tried with wp_reset_query(), no help. Im sure wordpress knows its a page. is_page(34, 'name', 'Name') wont help either.

A: 

Can you try to test your function with specific page:

if ( is_page(34, 'name', 'Name') ) 
{
    // your code here
}
Vasil Dakov
I've tried that, just now again too. Dont work
Greenie
A: 

Technically, Torok is wrong. is_page works perfectly fine outside the loop. I imagine it's not working because you're not using wp_register_script correctly. The dependencies need to be an array:

wp_register_script('jquery-validate', get_template_directory_uri() . '/js/jquery.validate.min.js', array('jquery'), '1.7');

That should do it.

EDIT:

As TheDeadMedic pointed out, you should also hook this on a later hook. The earliest one you should use is wp. I'd actually suggest using template_redirect, since that will only run if you're on the front end (unless you want to replace jQuery on the back end too).

John P Bloch
You should use the `wp` hook rather than `init` - WordPress hasn't parsed the request at the init stage, so it won't know if it's a page or not.
TheDeadMedic
Definitely right. I missed that the constructor for `WP_Query` doesn't parse the args if no information is passed to it. Good catch.
John P Bloch
Ahh of course. That makes sense, and it fixed my problem. Thanks a bunch :D And thanks for pointing out my non array dependencies, fixed too :)
Greenie