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.

I'm trying to add some Jquery scripts to my Wordpress site, and unfortunately I cannot link you to the site as I'm working locally. It seems that the Jquery file is being loaded, as well as the fancyboxStyle I created, but all of the other js scripts aren't loading.

functions.php

function my_init() {
    if (!is_admin()) {
        wp_deregister_script('jquery');
        wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');
        wp_enqueue_script('jquery');

    // load JS files from my theme

    wp_enqueue_script('easing', get_bloginfo('template_url') . '/js/jquery.easing-1.3.pack.js', array('jquery'), '1.0', true, $in_footer);  

    wp_enqueue_script('mouseWheel', get_bloginfo('template_url') . '/js/jquery.mousewheel-3.0.4.pack.js', array('jquery'), '1.0', true, $in_footer);

    wp_register_style( 'fancyboxStyle', get_bloginfo('template_url') . '/css/fancybox.css', true);

    wp_enqueue_style( 'fancyboxStyle' );

    wp_enqueue_script('fancybox', get_bloginfo('template_url') . '/js/jquery.fancybox-1.3.4.pack.js', array('jquery'), '1.0', true, $in_footer);

    wp_enqueue_script('fancyboxControls', get_bloginfo('template_url') . '/js/fancybox/fancyboxControls.js', array('jquery'), '1.0', true, $in_footer);


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

The files are all in the correct location, but the scripts do not load within the footer (or header for that matter).

Any help would be greatly appreciated.

Thanks,

Ashley

share|improve this question
    
Hi Roman, Thanks for the code but that doesn't seem to be working either. This time I don't actually have Jquery loaded either, but the fancyboxStyle is loading fine –  remi90 Sep 14 '11 at 11:17
    
For the next time. Please comment to my answer and use @Roman so I get a notification from WPSE. I've added a comment below. –  rofflox Sep 14 '11 at 14:35
add comment

1 Answer

up vote 0 down vote accepted

You have multiple errors here.

  1. You're including CSS and JS on the same and wrong hook
  2. For JS you could use wp_enqueue_scripts
  3. For CSS you could use wp_print_styles
  4. wp_register_script / wp_enqueue_script only accepts 4 parameters.
  5. You should only register your scripts and enqueue only the last one with its dependencies. Use the third parameter to define these.

Try out this code:

functions.php

add_action('wp_print_styles', 'wpse28490_enqueueStyles');
function wpse28490_enqueueStyles() {
    wp_register_style('fancyboxStyle', get_template_directory_uri() . '/css/fancybox.css');
    wp_enqueue_style('fancyboxStyle');
}

add_action('wp_enqueue_scripts', 'wpse28490_enqueueScripts');
function wpse28490_enqueueScripts() {
    if(!is_admin()) {
        // If our site is using SSL, we should load our external scripts also in HTTPS
        $protocol = (is_ssl()) ? 'https://' : 'http://';

        // jQuery
        wp_deregister_script('jquery');
        wp_register_script('jquery', $protocol . 'ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js', array(), '1.6.2', true);

        // Custom scripts
        wp_register_script('jquery-easing', get_template_directory_uri() . '/js/jquery.easing-1.3.pack.js', array('jquery'), '1.0', true);  
        wp_register_script('jquery-mouseWheel', get_template_directory_uri() . '/js/jquery.mousewheel-3.0.4.pack.js', array('jquery'), '1.0', true);
        wp_register_script('jquery-fancybox', get_template_directory_uri() . '/js/jquery.fancybox-1.3.4.pack.js', array('jquery'), '1.0', true);
        // fancyboxControls needs jquery, jquery-mouseWheel and jquery-fancybox, so we could pass these ids to the third parameter as an array.
        wp_register_script('fancyboxControls', get_template_directory_uri() . '/js/fancybox/fancyboxControls.js', array('jquery', 'jquery-easing', 'jquery-mouseWheel', 'jquery-fancybox'), '1.0', true);

        // Everything is registred, just enqueue our last script
        wp_enqueue_script('fancyboxControls');
    }
}
share|improve this answer
    
@remi90 The above script is working on a default installation. Please check your sourcecode. It could be that another script needs jQuery before the wp_footer hook and therefore include jquery in the header. Try disabling your plugins and test again. –  rofflox Sep 14 '11 at 14:37
    
It does indeed appear to work after disabling some plugins. Thank you Roman –  remi90 Sep 14 '11 at 14:57
add comment

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.