Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding jquery and custom scripts to a Wordpress theme

Doing some searching through Google, I came across the same bit of code over and over for adding jQuery to a from-scratch Wordpress theme.

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
   wp_deregister_script('jquery');
   wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
   wp_enqueue_script('jquery');
   wp_enqueue_script('my_script', get_bloginfo('template_url') . '/js/theme.js', array('jquery'), '1.0', true);
}

I've added this code to my functions.php file, and I created a js folder and made a theme.js file using the following syntax in it:

jQuery(function ($) {
    /* You can safely use $ in this code block to reference jQuery */
});

When I refresh my WP site, the theme.js file doesn't seem to be called. In Chrome Dev tools, it's not listed among the js files being rendered.

Am I using an outdated approach? How can I make my /js/theme.js file, using jquery, work for my theme?

like image 740
user1729506 Avatar asked Feb 13 '13 22:02

user1729506


People also ask

Can you use jQuery in WordPress?

WordPress comes bundled with jQuery and some essential jQuery libraries. WordPress theme and plugin developers can easily call jQuery in their own plugins and themes to add their own jQuery scripts. To call jQuery in WordPress theme or plugin, users need to add their own jQuery scripts and enqueue them in WordPress.

Can I use both JavaScript and jQuery together?

jQuery is a JavaScript library, so it operates on top of JavaScript. It cannot exist on its own, so you can't use one over the other. You can use just JavaScript or JavaScript and jQuery. jQuery was introduced to make development with JavaScript easier.

Can you add custom JavaScript in WordPress?

You can add custom JavaScript to your WordPress site either by using a plugin or by editing your theme or child theme's functions. php file. Using a plugin is the recommended technique if you don't want to edit your source files, as these plugins ensure that your custom scripts load in the right order.


1 Answers

First, wp_enqueue_scripts only runs on the frontend, so you don't need the is_admin() check.

Second, only de-register the default jQuery (bundled with WP) if you really know what you are doing. In your example, you are loading an outdated version from Google (current is 1.8.3, not 1.7.1). Also, see: Don’t Dequeue WordPress’ jQuery

Third, you should be using get_stylesheet_directory_uri, which is the correct function that will count for parent and child theme folders.

Finally, this code works ok in /themes/my-theme/functions.php:

add_action( "wp_enqueue_scripts", "my_js_so_14864221", 11 );

function my_js_so_14864221() 
{
    wp_enqueue_script( 
        'my_script', 
        get_stylesheet_directory_uri() . '/js/theme.js', 
        array( 'jquery' ), 
        '1.0', 
        true
    );
}

And your jQuery code in theme.js should be encapsulated like:

jQuery(document).ready(function($) {   
    // $('#your-stuff').animate().hide().whatever();
});
like image 51
brasofilo Avatar answered Sep 27 '22 01:09

brasofilo