WordPress.org

Ready to get started?Download WordPress

Forums

How to use jquery in wordpress? (7 posts)

  1. rjoshicool
    Member
    Posted 5 years ago #

    Hi,
    I am trying to use jquery in my wordpress theme but it is not working.
    Similar data written in an html file works but for the wordpress that thing is not working.
    Please help me out.
    Thanx in advance.

  2. Covi
    Member
    Posted 5 years ago #

    From WP2.5 (i think) jQuery it's included, u have some ways to call and inject jQuery in your theme:
    Directly or using the WordPress functions (recommended).

    1. Directly, it's the same way wich u has mentioned (<script src="...).
    2. Using wordpress, more elegant:
    See wp_enqueue_script() and wp_register_script().

    I hope this will be useful. ;)

  3. Roger Theriault
    Member
    Posted 5 years ago #

    and rather than the shortcut $ you should use jQuery("#foo")

  4. Doodlebug
    Member
    Posted 5 years ago #

    Can't get jQuery localScroll to work (so there is an animation between local anchor links on the same page):

    <?php wp_enqueue_script( 'scrollTo', 'wp-content/themes/abletonartists/js/jquery.scrollTo-min.js', array( 'jquery' ) ); ?>
    	<?php wp_enqueue_script( 'localScroll', 'wp-content/themes/abletonartists/js/jquery.localscroll-min.js', array( 'jquery' ) ); ?>
    	<script type="text/javascript">
    		$(document).ready(function() {
    			$.localScroll({
    				duration:1600
    			});
    		});
    	</script>
  5. Roger Theriault
    Member
    Posted 5 years ago #

    @Doodlebug - any error messages from Firebug?

  6. Doodlebug
    Member
    Posted 5 years ago #

    I've since done it direct like this and it works...
    <script src="<?php bloginfo('stylesheet_directory'); ?>/js/jquery-1.2.6.min.js" type="text/javascript"></script>

    I know this isn't the right way to do it - how can I use the in-built jQuery properly? All the documentation/writings concentrate on the perspective of plugin writers, not theme authors!

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    Your enqueue bits are fine, but make sure those calls happen BEFORE the wp_head action. This can be in a plugin or anywhere else. For a theme, puitting them in functions.php would work fine if you put them in a function hooked to the template_redirect action (to prevent them from also enqueuing in the admin screens).

    function my_theme_enqueue() {
    wp_enqueue_script('whatever'...
    ... etc, all your enqueues go here ...
    }
    add_action('template_redirect','my_theme_enqueue');

    Your actual script output would need to be elsewhere. In the relevant template, I expect.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags