Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Why doesnt this work ? I am trying to selectively load scripts but the second if statement is not loading them where it should.

function my_init() {
    if (!is_admin()) {

        wp_deregister_script('jquery');
        wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js', false, '1.5');
        wp_enqueue_script('jquery');

            if (in_category('41') )  {
            wp_register_script('validator', '/wp-content/themes/myTheme/library/jquery.validationEngine.js', array('jquery'), '2.0', false);
            wp_enqueue_script('validator');

            wp_register_script('vEngine', '/wp-content/themes/myTheme/library/jquery.validationEngine-en.js', array('validator'), '2.0', false);
            wp_enqueue_script('vEngine');
            }

    }   


}
add_action('init', 'my_init');

If I remove the second if statement for that category it works but I want to restrict it to a single category or even an is_single('123')

What am I doing wrong?

share|improve this question
    
How does it not work? –  EAMann Feb 22 '11 at 20:28
    
It does not load the validator scripts –  zac Feb 22 '11 at 20:45
1  
change 'init' to 'wp_print_scripts' –  Bainternet Feb 22 '11 at 21:24

2 Answers 2

up vote 1 down vote accepted

I suspect you're doing this too early. The loop isn't setup yet when init is run, so you need to hook it somewhere else. wp_print_scripts and wp_print_styles or get_header are always nice choices. ;-)

share|improve this answer
    
Or even better would be the wp_enqueue_scripts hook... :) –  t31os Feb 22 '11 at 21:21
    
Thanks that is what I needed. –  zac Feb 22 '11 at 21:44

I poked around, and it seems that the Codex reference that you can use conditional tags like is_category() in the init hook is incorrect. However, this code does work just fine:

function my_init() {
    if (!is_admin()) {

        wp_deregister_script('jquery');
        wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js', false, '1.5');
        wp_enqueue_script('jquery');
    }   
}
function my_scripts() {
    if (in_category('41') )  {
        wp_register_script('validator', '/wp-content/themes/myTheme/library/jquery.validationEngine.js', array('jquery'), '2.0', false);
        wp_enqueue_script('validator');

        wp_register_script('vEngine', '/wp-content/themes/myTheme/library/jquery.validationEngine-en.js', array('validator'), '2.0', false);
        wp_enqueue_script('vEngine');
        }
}
add_action('init', 'my_init');
add_action('wp_print_scripts', 'my_scripts');
share|improve this answer
    
Your wp_print_scripts is missing an s ... ;) –  t31os Feb 22 '11 at 21:22
    
Too many rapid-fire edits ... It's been a long day :-) –  EAMann Feb 22 '11 at 21:27

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.