Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Help w/ jQuery scripts & wp_enqueue_script WordPress


Hey there,

I'm having some issues registering jQuery scripts. I'm a designer who is fairly new to coding, rather proficient in HTML and CSS, but Java is still new to me.

I am trying to properly register a few Js scripts in a client site:

[[LINK href=""]][[/LINK]]

The first is a simple jQuery call to action bar for the header:

[[LINK href=""]][[/LINK]]

(documentation and code is there)

It seems the theme has a custom functions-header.php file that .functions.php call .js from as well as the standard functions.php calling scripts. However, I am going nuts as to where/how to setup and reference the code(s). The functions-header.php is located in the theme/includes file.

I'd also like to call a separate .css file for the plugin as opposed to putting it all in style.css.

I know how to call stylesheets via: <link href="" rel="stylesheet" type="text/css" />

but according to WP codex it should also be done using the enqueue script, so I'd like to register that as well. I am lost.

Here's the functions.php

[[LINK href=""]][[/LINK]]

and header.php

[[LINK href=""]][[/LINK]]

and functions-header.php

[[LINK href=""]]and functions-header.php[[/LINK]]


Answers (2)


Plugarized answers:

add the following to line 170 of your theme's functions.php or header functions for enqueueing the required js scripts first, upload the Woahbar js to the /js/ folder.

wp_register_script('jquery', '', false, '1.9.1');

wp_register_script('jquery-ui', '', array('jquery'), '1.10.1');

wp_register_script( 'woahbar', get_template_directory_uri() . '/js/WOAHbar.js', array( 'jquery' ), false, true );


As for the styles, I would suggest you add it to the main style as it reduces the http requests, meaning that the page loads faster, you can check at gtmetrix or pingdomtools to see your website load speed and http requests number.

<strong>more information about en-queueing scripts and registering them:</strong>

<strong>If you need particular version of the google cdn js scripts go here and change the version accordingly</strong>

cunningfox comments:


I updated functions.php as above, added WHOAbar.js to /js but not working. Css has been added to bottom of style.css. From the documentation:

<blockquote>STEP 1 - CALL MAIN JS SCRIPTS:

You will need to add the following scripts BEFORE the </head> tag of your webpage for any of the following to work properly:

<script src="" type="text/javascript"></script>

<script src="" type="text/javascript"></script>

*NOTE: You may already be calling one or both of these scripts in the page for other javascript purposes, so you may need to delete one or both lines for the WOAHbar to work properly. </blockquote>

As you posted, do I need to call a specific version of java for each plugin? Is that what the _register_script & _deregister_script do?

<strong>Do I need to 'register' (via the helpful Google library link) version 1.7.1 or 1.8.16 to get this to work? Is Jquery not backwards compatible w/ previous versions?</strong>

Believe me, I read both those codex articles about 5 times each, as well as countless articles online. I have no problem with Java in HTML, but adding new scripts into WP is a fairly new endeavor for me.

What is:

rray( 'jquery' ), false, true );

Thank you.

Plugarized comments:

<blockquote>array( 'jquery' )</blockquote>

the array means that the file requires jquery to run, 'false' is the parameter for an array and true means that it should load the js file in the footer.

I would recommend to read the listed codex links, they clear things out and explain these parameters in depth.

Plugarized comments:

sorry i didnt read the rest of your message, basically wordpress by default loads jquery, however the versions on google cdn are always uptodate, you can disable the code for that if you want to use local file.

most plugins work with default version of jquery so there is no need for a specific version for each plugin, however some may use new functions available in newer versions.

Plugarized comments:

If you want to grant me access, I will check what the issue is, please provide credentials via PM

Plugarized comments:

You can check the following jsfile to see it working (note, looks messy no images)

Plugarized comments:

Get rid of the "display:none" style in the following <div class="woahbar" style="display: none;">