GeekAmongUs

  • Home
  • About
  • Blog
  • Contact

WordPress and jQuery issues

Posted by willc on April 13, 2011
Posted in: Tweaks, Web Design, WordPress.

First, I don’t remember where I found this along the way, but this is a smart and efficient way to include jQuery in your WordPress themes. Just add this to your theme’s functions.php file:

// ADDING JQUERY
add_action( ‘init’, ‘jquery_register’ );
add_filter( ‘script_loader_src’, ‘jquery_unversion’ );

function jquery_register() {

if ( !is_admin() ) {
wp_deregister_script( ‘jquery’ );
wp_register_script( ‘jquery’, ( ‘http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js’ ), false, ’1.x’, false );
wp_enqueue_script( ‘jquery’ );
}
}

// remove version tag to improve cache compatibility
function jquery_unversion( $src ) {

if( strpos( $src, ‘ajax.googleapis.com’ ) )
$src = remove_query_arg( ‘ver’, $src );

return $src;
}

Now, the issue I have been having is that when I try to add your own jQuery scripts or functions in your theme’s header.php file, I always get this error:

“$ is not a function”

What?  Yes it is, dammit!

Turns out WordPress has reserved the $ character, so you have to rename all occurence of $ to ‘jQuery’.  Example:

Before

<script type=”text/javascript”>
$(document).ready(function() {
$(‘.slideshow’).cycle({
fx: ‘fade’,
timeout: 4000,
speed: 2000
});
});
</script>

After

<script type=”text/javascript”>
jQuery(document).ready(function() {
jQuery(‘.slideshow’).cycle({
fx: ‘fade’,
timeout: 4000,
speed: 2000
});
});
</script>

Hope that saves someone the hours of frustration I have had with this.

Share this:

  • Twitter
  • Facebook
  • Google +1

Posts navigation

← Apple Mail “smtp.xxxx.com:xxxxx” offline
Quick overview: H vs S vs E IPS monitors →
  • Search

  • Great Stuff

  • Friends

    The best company for computer repair in Asheville you will find are my buddies at Christopher's Computers..
  • All About You

    IP Address: 23.22.212.158
    Place: , ,
    Zip Code:
    Lat:
    Long:

  • Donate

    If you found something useful here on Geekamongus, please consider donating some mulah. It's quite a good motivator!
  • Categories

    • 0-day
    • Apple
    • Apps
    • Audio
    • Backups
    • Black Hat
    • CPanel
    • CSS
    • Databases
    • Email
    • Facebook
    • Firefox
    • Freeware
    • Games
    • Geeky Greats
    • Google
    • Hacks
    • HTML
    • jQuery
    • Linux
    • Mac
    • Microsoft
    • MySQL
    • Network
    • News
    • Photoshop
    • Practical Security
    • Privacy
    • RedHat
    • Samba
    • Security
    • Social Networking
    • Spam
    • SSH
    • Tweaks
    • Uncategorized
    • Virtualization
    • VNC
    • Web Browsers
    • Web Design
    • WordPress
  • Archives

    • March 2013
    • January 2013
    • December 2012
    • August 2012
    • June 2012
    • May 2012
    • December 2011
    • May 2011
    • April 2011
    • March 2011
    • January 2011
    • July 2010
    • June 2010
    • May 2010
    • March 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
Proudly powered by WordPress Theme: Parament by Automattic.