Problem with is_page() conditional in WordPress

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.

Read More

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.

Related posts

Leave a Reply

3 comments

  1. 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).