Category Archives: Code

Including WordPress’s comment-reply.js (the right way)

Since threaded comments were enabled in WordPress 2.7, most themes include the following line in header.php

<?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>

This code checks if the visitor is browsing either a page or a post and adds the JavaScript required for threaded comments if they are.

I prefer a slight variation

<?php
if ( is_singular() && comments_open() && get_option('thread_comments') )
  wp_enqueue_script( 'comment-reply' );
?>

My variation checks if the visitor is browsing either a page or a post, if comments are open for the entry, and finally, if threaded comments are enabled. If all of these conditions are met, the JavaScript required for threaded comments is added.

If you run your wp_enqueue_script calls in functions.php, as I do, this is the code to use:

<?php
function theme_queue_js(){
if ( (!is_admin()) && is_singular() && comments_open() && get_option('thread_comments') )
  wp_enqueue_script( 'comment-reply' );
}
add_action('wp_print_scripts', 'theme_queue_js');
?>

The call is added to the wp_print_scripts action as is_singular and comments_open are unknown during the init action.

 

Note: I’ve¬†written a plugin to make the comment-reply JavaScript unobtrusive, it’s call Rapid Comment Reply.

Caching on the Google AJAX Libraries API

Using the Google AJAX Libraries API, there are several options for specifying the version numbers of the library you wish to use, for example the following URLs all point to the latest version – at the time of writing – of jQuery.

  1. http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js,
  2. http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js, and,
  3. http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js

The first uses the latest release in the version 1 tree, the second the latest in the version 1.3 tree, and the third the release 1.3.0 exactly. What I found interesting – and discovered accidently – is the browser caching times for each of these urls; the first two are cached for an hour, the third for twelve months.

The vastly different caching times make perfect sense, in the first two cases, the developer is expecting an upgrade to the latest version, and doesn’t want to wait up to twelve months for it; in the third case the developer doesn’t expect an upgrade so an extended caching period has no effect.

To take full advantage of the Google servers, as described in a recent article by Dave Ward, the caching times suggest it’s best to specify the full version of the library you wish to use.

google.load Method – Update Jan 24, 2009

Continue reading Caching on the Google AJAX Libraries API

JavaScript Equal Height Columns

The desire for equal height columns in a CSS layout is nothing new; there are many solutions available, some use JavaScript, others use CSS with negative margins, and then, there’s the faux columns method using background images. All of these methods have their place as perfectly valid solutions, and, depending on the situation, may be the best solution available. Continue reading JavaScript Equal Height Columns

Base JavaScript File

Upcoming posts on JavaScript will include references to functions in my base JavaScript file; rather than explain these functions each time, they’ll be detailed in this post for future reference.

There are many pre-written JavaScript examples and functions available, so it’s important to note that all of these functions have been written by others, some include slight changes; all moved to the namespace PWCC, as you’ll see in the source code.

Continue reading Base JavaScript File

Don’t start with a reset.css

Earlier this year, Jonathan Snook wrote an article on why he doesn’t use a reset.css in which he referred to Eric Meyer’s reset, a short time later, Eric Meyer responded with an article of his own. Unlike many discussions on the web, it wasn’t a mudslinging match, but a sincere discussion of the tools available to web developers.

My initial thoughts were that a reset style sheet, combined with a base style sheet, was a helpful place to start any web project as it reduces the incidence of unexpected results. I thought it was important for the developer to have her own reset and base styles; blindly adopting someone else’s reset and base style sheets will just lead to a different set of surprises.

Continue reading Don’t start with a reset.css