Include Jquery,Scriptaculous, or any 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.

14 Comments

  1. SaurooonNo Gravatar
    Jul 14, 2009

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

    • snileshNo Gravatar
      Jul 14, 2009

      @Saurooon
      I am from india.

  2. cathyNo Gravatar
    Jul 14, 2009

    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.

  3. AnnaHopnNo Gravatar
    Jul 18, 2009

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

    • snileshNo Gravatar
      Jul 18, 2009

      @AnnaHopn
      Thanks for your comment. I will make sure to update my blog regularly.

  4. MartinOttmanNo Gravatar
    Jul 23, 2009

    I cannot believe this will work!

  5. JohnNo Gravatar
    Jul 24, 2009

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

  6. AlexAxeNo Gravatar
    Jul 24, 2009

    Thanks for article. Everytime like to read you.

  7. Sed priosipNo Gravatar
    Aug 18, 2009

    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/

    • snileshNo Gravatar
      Aug 18, 2009

      @Sed priosip Thanks for your comment

  8. UldisNo Gravatar
    Dec 02, 2009

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

  9. ahmet korkmazNo Gravatar
    Dec 12, 2009

    http://agricultureguide.org

    you can see this ticker here too, thanks snilesh.

  10. leeptonNo Gravatar
    Dec 14, 2009

    Awesome plugin,

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

    regards,

    Leepton

  11. MohammedNo Gravatar
    Dec 20, 2009

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


Trackbacks and Pings

Leave a Reply