0

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?

3
  • How does it not work?
    – EAMann
    Feb 22, 2011 at 20:28
  • It does not load the validator scripts
    – zac
    Feb 22, 2011 at 20:45
  • 1
    change 'init' to 'wp_print_scripts'
    – Bainternet
    Feb 22, 2011 at 21:24

2 Answers 2

1

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

1
  • Or even better would be the wp_enqueue_scripts hook... :)
    – t31os
    Feb 22, 2011 at 21:21
1

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');
2
  • Your wp_print_scripts is missing an s ... ;)
    – t31os
    Feb 22, 2011 at 21:22
  • Too many rapid-fire edits ... It's been a long day :-)
    – EAMann
    Feb 22, 2011 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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