Preventing jQuery conflicts in WordPress

By Sean Sanders on March 30, 2010 at 4:36 pm in Development,Technical

jquery

Okay, it’s programming pet peeve time. Well, specifically web programming pet peeves. And really just one, so forget the plural there. Here goes nothing.

If you do any kind of web programming at all, you’ll have heard of jQuery, which is (according to this source: http://trends.builtwith.com/javascript/JQuery) the most popular Javascript library in use today. It’s got several things going for it. It’s powerful. It’s flexible. It’s easy to use. It’ll even clean your house and do your dishes if you ask nicely. OK, maybe not that last one, but I’m sure it will be in a future release.

Being as powerful as it is, it comes as no surprise that a large number of WordPress plugins use jQuery for all kinds of things. If you are a theme or plugin developer chances are you will frequently make use of jQuery in this development process.

The problem with so many developers using jQuery is using the traditional method of including it can cause conflicts between versions. Typically, you would load jQuery in one of a couple of ways.

If you have a local copy:

<script type="text/javascript" src="_js/jquery.js"> </script>

Or if you prefer Google’s CDN (Content Distribution Network) version:

<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js”></script>

The problem with directly placing these in your theme’s header, or using the add_action function as a plugin writer, is yours is likely not the only part of the end-user’s WordPress installation which will want to use jQuery. If you have multiple places in the website trying to load jQuery you end up with at best redundancy in your code and at worst a conflict which breaks your website.

Fortunately, if you want to use jQuery without causing conflicts then WordPress will do of the work for you. WordPress already includes its own version of jQuery, so if you’re happy with using the included version, all you need is this php function:

<?php wp_enqueue_script('jquery'); ?>

WordPress inserts all the queued scripts during the wp_head hook, so you need to call this function before that hook runs. If you’re a theme developer, it’s sufficient to insert this function call anywhere in your head section (assuming you follow the practice of always placing wp_head() at the end). If you’re a plugin developer, you’ll need to make use of the init hook.

<?php
	function my_init_method() {
		wp_enqueue_script('jquery');
	}
add_action('init', 'my_init_method');
?>

This will make sure the script is queued before wp_head is run. Now say you don’t like WordPress’s included version and would instead like to use a different version. Here is how you would change it to use the CDN version:

<?php
function my_init_method() {
	wp_deregister_script( 'jquery' );
		wp_register_script(   'jquery',
		'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js');
	wp_enqueue_script(‘jquery’);
}
add_action('init', 'my_init_method');
?>

One last thing to note: WordPress loads jQuery in “no conflict” mode. A good description of the details of this mode are available here: http://api.jquery.com/jQuery.noConflict/. What this means for most users is just you’ll want to use the jQuery alias instead of the normal shortcut of $. This is to prevent conflicts with other scripts which use the same shortcut. Instead of looking like this:

$(‘.lameClass’).addClass(‘awesomeClass’);

Your code will look like this:

jQuery(‘.lameClass’).addClass(‘awesomeClass’);

You can see that WordPress has built in the capability to handle pretty much any scenario you could come across; you can even load different versions of jQuery and give them different aliases if you really need to. So now that everybody knows, I’m not going to see any more jQuery conflicts in WordPress. Right? Please?

You can find more information on queueing scripts and a list of plugins WordPress has handles for in the WordPress codex at http://codex.wordpress.org/Function_Reference/wp_enqueue_script.

Cooookie?

By Elyssa Dolinger on March 18, 2010 at 4:03 pm in Comic,General

We always thought it was funny that those little bits of browser information were called cookies.

I dedicate this little comic to Cookie Monster. Even though they make you eat cookies in moderation now, you will forever hold a place in our heart.. I’ll save an internet cookie just for you, big blue.

cisforcookie

(Click to view full size.)

Email Etiquette Part 2

By Elyssa Dolinger on March 10, 2010 at 4:23 pm in Email

Continuing on with our Email Etiquette series, I have 3 quick tips. The sandwich method, active voice and the use of sentence case.

First, the sandwich method. It goes a little something like this:

compsand

When you sandwich your critique or bad news in good news, it makes people a lot more receptive to what you have to say. It looks less like an attack and more like the helpful-yet-unsavory idea it is meant to be. This isn’t a way to disarm people so that you can sucker punch them; this just a little something you can try to build respect and trust with a person you may not speak to a lot outside of email. Grease the wheels with politeness and things will go a lot smoother.

Another subtle change that can make you more personable is the use of active voice. Oh no. Grammar. Active voice just means that you try to use use the active role of a verb whenever possible.
Example:

Your updates will be processed by the end of the week.

My Response?

WebMeh. A robot sent me a response. I wonder if my updates will ever go through?

We will process your updates by the end of this week.

My Response?

Web I look at the calendar. Oh? Really? The end of this week? That’s fast.


An easy way to tell the difference between active and passive voice? Look at who the subject is. In the first example, it’s the ‘updates.’ The updates are processed. In the active example ‘we’ process updates. It’s a lot more personable when there is a person involved.

My last tip should be pretty self explanatory. PLEASE DON’T USE ALL CAPS FOR YOUR ENTIRE EMAIL. EVEN IF YOU ARE NOT SHOUTING, IT SOMETIMES APPEARS THAT YOU ARE.

Twitter Resources

By Tim Priebe on March 3, 2010 at 2:09 pm in Link Roundup,Social Networking

PrintUnlike some other social networking sites, Twitter does not have third party applications on their website. Instead, they allow those to be hosted on other websites, and let those tie directly into Twitter. Here are a few of those Twitter resources I’ve found useful

TweetBeep.com

tweetbeep

If you are familiar with Google Alerts, then you’ll understand TweetBeep.com, which bills itself as Google Alerts for Twitter. Basically, you can enter search terms, and TweetBeep.com will email you tweets that match the search when they show up on Twitter.

TwitBacks.com

twitbacks

If you’re not a graphic designer yourself, TwitBacks.com is a good resource for getting a background image for your Twitter account that’s more customized than the backgrounds Twitter offers by default. They have over 80 backgrounds, and you can customize them online with your social networking links, a longer biography than Twitter allows, and even add a picture of yourself or your logo.

SocialOomph.com

socialoomph

If you’re looking to schedule tweets, automatically follow those who follow you or automatically send direct messages to new followers, SocialOomph.com is the choice for you.

Twitter Feed

twitterfeed

Tweeting about your latest blog entry is made automatic with Twitter Feed. Just enter in any RSS feed address, customize the settings if necessary, and every time you blog it will be pushed automatically out to Twitter. Of course, other RSS feeds will work as well.

Twitter Grader

twittergrader

Wanting to see how well you’re using Twitter? Twitter Grader is an automated tool from the guys that brought you Website Grader. While this type of tool should be taken with a grain of salt, it is neat to see how you rank compared to others in your town.

Your Twitter Karma

twitterkarma

Your Twitter Karma will give you a list of all the people who you’re following or who are following you, and will let you filter the list. A good tool if you want to see who’s following you that you’re not following, or vice versa. It was extremely useful when they would let you bulk unfollow people you were following who weren’t following you, but apparently Twitter asked them to remove that feature. Also, it’s a bit slow and the site is as ugly as sin, but it’s useful otherwise.

Know of some other great Twitter resources you’ve found useful? Please let us know!