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.

Forgive me for knowing next to nothing about coding. BUT, I am calling jQuery in functions.php, which I have been told is the best way to include jQuery:

//Get jQuery
function modify_jquery() {
    if (!is_admin()) {
        // comment out the next two lines to load the local copy of jQuery
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', false, '1.8.3');
        wp_enqueue_script('jquery');
    }
}
add_action('init', 'modify_jquery');

I am now trying to add a jQuey function from PaperMashup.com which looks like this.

I was under the impression if I simply copy and pasted the above jQuery function into the following code it would work, but it does not. How do i incorporate this function?

jQuery(function ($) {
    /* PASTE CODE HERE */
});
share|improve this question
    
How are you including that code? Where is that code? What are you doing with it? –  s_ha_dum Jan 4 '13 at 15:32
    
I was including it like this snippi.com/s/6oy88nc by pasting it into functions.php I have no idea how to incorporate it properly. –  AndrettiMilas Jan 4 '13 at 15:37
    
Gee guys, thanks for the down vote. I'm confident nobody has ever searched for how to properly enque jQuery codes... –  AndrettiMilas Jan 4 '13 at 15:46
add comment

1 Answer

up vote 5 down vote accepted

First, don't unregister and reregister jQuery. There's no reason to do that at all. Second, you're enqueuing on the wrong hook. Instead, use this in your functions file:

// Enqueue Scripts
function wpse78227_enqueue_scripts() {
    wp_enqueue_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts', 'wpse78227_enqueue_scripts' );

Now, to include that script you need to do a couple of things.

  1. Create a new file in your theme for the script (i.e. jquery.show-hide.js)
  2. Copy that jQuery function into this new file
  3. Enqueue the script file as you would any other

This would then give you:

// Enqueue Scripts
function wpse78227_enqueue_scripts() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'jquery-show-hide', get_template_directory_ui() . '/js/jquery.show-hide.js', array( 'jquery' ) );
}
add_action( 'wp_enqueue_scripts', 'wpse78227_enqueue_scripts' );

Assuming your script file is now located in /wp-content/themes/your-theme-name/js/jquery.show-hide.js. It also lists jQuery as a dependency, so you could forgo enqueuing jQuery at all if you want to (When WordPress attempts to load the plugin, it sees the dependency on jQuery and automatically loads it first if it's not already loaded).

share|improve this answer
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.