Include Jquery,Scriptaculous javascript libraries in wordpress theme

Home » Blog » Include Jquery,Scriptaculous javascript libraries in wordpress theme

Most of the wordpress programmer don’t know that most of the javascripts libraries are already loaded in to the wordpress, So if you want to use them in your theme you only have to call them using the wp_enqueue_script

When you want to include javacript libraries in your theme you have to call the wp_enqueue_script() function using simple parameters.

<?php wp_enqueue_script( $handle, $src, $deps, $ver );
?>

Parameter

$handle
(string) (required) Name of the script. Lowercase string.

Default: None

$src
(string) (optional) Path to the script from the root directory of WordPress. Example: “/wp-includes/js/scriptaculous/scriptaculous.js”. This parameter is only required when WordPress does not already know about this script.

Default: None

$deps
(array) (optional) Array of handles of any script that this script depends on; scripts that must be loaded before this script. false if there are no dependencies. This parameter is only required when WordPress does not already know about this script.

Default: None

$ver
(string) (optional) String specifying the script version number, if it has one. Defaults to false. This parameter is used to ensure that the correct version is sent to the client regardless of caching, and so should be included if a version number is available and makes sense for the script.

Default: None

So as all the javascripts libraries like Jquery,Scriptaculous,Tiny MCE,ThickBox are already included in wordpress, you don’t have to upload and include in your wordpress theme again, so you have to just include them in the wordpress theme using the wp_enqueue_script().

So when you want to include jquery in your theme you have to call

Load the jquery script:

 <?php
function my_init_method1() {
wp_enqueue_script('jquery');
}

add_action('init', my_init_method1);
?>

Load the scriptaculous script:

 <?php
function my_init_method() {
wp_enqueue_script('scriptaculous');
}

add_action('init', my_init_method);
?>

This function can be used to include your own javascript files using

<?php
wp_enqueue_script('handle', 'src', 'dependencies');
?>

<?php wp_enqueue_script('jquery-name', 'http://yoursite.com/wp-content/themes/your_theme/jquery/jquery-ui-tabs.js', 'jquery'); ?>

/*
Parameters:
handle= Is a unique name that you want to give to your script.
src= Is the script url
dependencies= Optional parameter that allows you to specify dependencies. Like which script must be loaded before this script. In some jquery plugins you have to load other plugins first. In that case this function is very useful
Example of dependencies
*/

Note: This function will not work if it is called from a wp_head action, as the <script> tags are output before wp_head runs. Instead, call wp_enqueue_script from an init action function (to load it in all pages), template_redirect (to load it in public pages only), or admin_print_scripts (for admin pages only). Do not use wp_print_scripts (see here for an explanation).

The function will also ensure that the same script will not be loaded again, Which means if you include jQuery in your theme, and you install a plugin that tries to load jQuery, it will only be loaded only once.

Tags: , , , ,

20 Comments on "Include Jquery,Scriptaculous javascript libraries in wordpress theme"

  1. Saurooon says:

    Hi there,
    Where are you from? Is it a secret? :)
    Saurooon

    Reply →
  2. cathy says:

    Sorry for such a dumb question, but do you put the wp_enqueue_script() function information in the theme’s functions.php file?

    Secondly, how do you call the function in the template file where you want to run the script?

    I’m trying to use a jQuery accordian menu in wp 2.8 and am having a real hard time getting it to work.

    Reply →
  3. AnnaHopn says:

    Greatings, Thank you! I would now go on this blog every day!
    Thanks

    Reply →
  4. MartinOttman says:

    I cannot believe this will work!

    Reply →
  5. John says:

    Great article, I will definately try this on my wordpress projects

    Reply →
  6. AlexAxe says:

    Thanks for article. Everytime like to read you.

    Reply →
  7. Sed priosip says:

    I just wanted to say WOW! your site is really good and i’m proud to be one of your surfers
    please review my first site [url=http://dvdclubadult.com/]here[/url] – http://dvdclubadult.com/

    Reply →
  8. Uldis says:

    Nice work! Your plugin is realy great and i will use it for my blog http://www.laulibas.lv . Thanks snilesh

    Reply →
  9. http://agricultureguide.org

    you can see this ticker here too, thanks snilesh.

    Reply →
  10. leepton says:

    Awesome plugin,

    it’s possible use shortcode ie [newstiker] for add it in any confortable place into pages.

    regards,

    Leepton

    Reply →
  11. Mohammed says:

    Thanks for this plugin, and your blog is very nice :)
    good luck

    Reply →
  12. Rob says:

    Hi, I’ve just installed and activated the plugin, what do I need to do to see on my website?

    I’d like it to replace my static image header, is that possible? (I built my WP site with Artisteer)

    Thanks so much

    Reply →
    • snilesh says:

      Yes you can replace your static header with this slide show.

      As you said you have to replace header image with the slideshow then you have to edit the header.php file just open the header.php from your currently activated theme’s folder.

      Just add following lines in that header.php file at the position where you want to show the slider

      <?php if(function_exists('wp_content_slider')) { wp_content_slider(); } ?>
      

      Login to your wp-admin and check “Content Slide” Options page to configure the slider.

      let me know if you have any issues.

      Reply →
  13. Rob says:

    Thanks for the help. But I’m still learning php and am not sure where in the header to paste the code.

    If you could help me I’d be very grateful.

    my header.php file looks like this:

    <html xmlns="http://www.w3.org/1999/xhtml&quot; >

    <meta http-equiv="Content-Type" content="; charset=” />

    <script type="text/javascript" src="/script.js”>
    <link rel="stylesheet" href="” type=”text/css” media=”screen” />

    <link rel="alternate" type="application/rss+xml" title="” href=”" />
    <link rel="alternate" type="application/atom+xml" title="” href=”" />

    <link rel="pingback" href="” />

    <a href="/”>

    Reply →
  14. David says:

    Excellent, so that snippet goes in the theme functions.php under the theme folder, right? Since Scriptaculous and Prototype are often paired, and I found out both are included in wordpress, this would work, right?:

    Thanks!

    Reply →

Got something to say? Go for it!

How to remove the http://t.co/XhUQ8T6p link from Login / Register Page: On self hosted wordpress blogs wordpress ... http://t.co/LWDOuEfq - 1 week ago