Easy way to check if jQuery is already enqueued

Had a plugin wreaking some havoc today because it was overloading jquery.js with a minified, older version. Plugin authors: There’s a really simple way to check if jQuery or a jQuery library is already registered and enqueued. This covers really obscure edge cases where a plugin may have de-registered WordPress’s default scripts.

The offending code:

wp_register_script('myjquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js', true, '1.6.4', false);
wp_enqueue_script('myjquery');

The fix:

// If jQuery isn't already enqueued, register and enqueue it
if ( ! jQuery ) {
	wp_register_script('myjquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', true, '1.7.2', false);
	wp_enqueue_script('myjquery');
}

Template parts as widgets

On a project this morning, I had a need to pull in some formatted content into a sidebar. My first inclination was to A) Use a plugin like Query Posts to pull it in or B) Write up a custom widget we could use. I opted for neither.

The thing is, we’ve already got the content formatted into a template part file and the effort involved to roll it into a widget really wouldn’t be worth the effort. This isn’t a client project, so it doesn’t necessarily have to be pretty on the back-end … it just has to work. So I opted for using get_template_part().

The nice thing about using get_template_part() to include WordPress templates is that you can call them from anywhere in your theme. And with a bit of help from something like Otto’s PHP Code Widget plugin, you can even include them in sidebars! Some people might see it as a bit of a janky way to accomplish it, but it got the job done.

It’s pretty straightforward:

  1. Install PHP Code Widget
  2. Create a template part in your theme/child theme directory
  3. Add an instance of the PHP Code idget to a sidebar via Appearance > Widgets
  4. Insert a get_template_part() call to include your template part

New local-to-live deployment plugin falls a bit short

A new local-to-live deployment plugin called WP Live Server Deploy was brought to my attention last week by a post over at WP Force.

The plugin boasts an automated set of features to handle the menial tasks of MySQL dumps + find & replace, plus handling your file transfers. Out of the box it seemed pretty promising. I’d say the biggest disappointment out of the box is a complete lack of SFTP/SSH support for the transfer. You can only do so much over FTP alone and the plugin failed miserably to handle the MySQL part of the equation.

Along that vein, there’s a so-called “Manual” option for handling the MySQL dumps. This would be fine, except that it doesn’t work — it nets an empty SQL file. I’d say @sagetarian has some work to do yet, but I’m looking forward to seeing another iteration. You can download the plugin via the WP.org repo and/or follow development on Github.

The Quest to Manage Code

In the last several years I’ve fostered a particularly nasty habit of neither organizing nor collecting all of my code snippets into a centralized place.

I’ve gotten into the habit of using live sites as living repositories that I just visit when I need something. A friend of mine just started using something called JCodeCollector (Mac) I thought I’d take for a spin.

If I’m not mistaken, TextMate also has a snippet manager built into it. I guess I’ll check back with how my “organizational” project goes in a future post. What do you use to manage your code snippets?