wp-content in Code is a Tell for Common WordPress Coding Mistakes

Regularly while reviewing themes and plugins, I’ll see URLs or paths that include ‘wp-content’. This is a often a hint of a WordPress coding mistake.

Consider this simple example:
<link rel="stylesheet" href="http://example.com/wp-content/themes/default/style-ie.css" type="text/css" media="screen" />

If as part of a migration or server change, WordPress ends up in a sub-directory this will break.

For file system based access, if I see ABSPATH . '/wp-content/ … things are likely OK, though if WP_CONTENT_DIR or similar are changed, this will also break.

I’m tempted to use the constant STYLESHEETPATH or is it TEMPLATEPATH? But, those are static variables initialized early in the WordPress run.

I’ve been meaning to write this article for a while. Serendipitously, when I went to write a first draft of it, Mark Jaquith‘s had just published “Force CSS changes to “go live” immediately“.

In the comments, demetris suggests using STYLESHEETPATH, but Will Norris quickly points out “sure, but you still have the same problem of bypassing any plugins that may be using the ’stylesheet_directory’ hook to pull the stylesheet from some place else entirely.”

Mark’s solution beautiful illustrates how to get the the URL location of the theme’s files and also directly the URL for the main stylesheet style.css:

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); echo '?' . filemtime( get_stylesheet_directory() . '/style.css'); ?>" type="text/css" media="screen, projection" />

What Will describes is exactly what WordPress.com depends on. We have a similar, but much more complex solution to deal with the CSS in the content delivery network (CDN) going stale on updates, and also some web browser’s with their funky caches not keeping up. Our solution is both for WordPress’s front end and dashboard (also dealing with the various ways CSS can be called or @imported).

I do have one niggle about Mark’s solution, and it’s the same with most WordPress code out there, the use of bloginfo(). Strings within strings within strings within strings give me headache, so to ease readability and make it more verbose replace:

bloginfo('stylesheet_url'); with
echo get_stylesheet_uri();

bloginfo('stylesheet_directory'); with
echo get_stylesheet_directory_uri();

If you look up in the codex get_stylesheet_directory() you’ll see that would be the file system path, which is confusing given the behavior of bloginfo('stylesheet_directory'); returning the URL. This presents another reason why I favor the consistency of always using the specific getter function calls. Also, when I’m using bloginfo() calls, I have a tendency to get in a mind set where I think I’m directly retrieving the option in the database (accessing directly the info shown in wp-admin/options.php). By using the specific getter function, I remember that there are hooks (actions, filters) that might be triggered.

Some of the other areas, I see wp-content is in CSS style sheets themselves. Relative paths are almost always the correct solution.

Far more painful for me are the plugins that assume where plugins are, because this can take quite a bit of debugging to unwind the assumptions.

That is a topic for me to postulate on another day. Will has written the very relevant “WordPress Plugin Pet Peeve #2: Direct Calls to Plugin Files“. Duane Storey has taken a crack at a “WordPress Plugin Checker“  for these and other common problems (which I’m certain he would enjoy feedback on).

Ferry to the New Vancouver Apple Store?

The email’s subject read “Grand Opening: The Apple Store, Pacific Centre.” and there is a prominent Get Directions button, but I’m not sure they would have sent it to me if they had considered where I live (if it was a person manually sending the invites). It’s a Google estimated 2 hours 47 mins “drive” includes the 2hr $55 (car included) ferry ride. Continue reading

WordPress and your Problems at MooseCamp!

Bloggers and problems enter, only bloggers exit!

Friday, Feb 22, day 1 of the 4th annual Northern Voice including an “internet bootcamp” for people new to blogging, Facebook, podcasting, wikis, and more. It is an awesome logging conference being held again at the Forestry Sciences Centre at UBC! It’s Tim Bray’s “favorite little blogger conclave.” This conclave is really for everyone, but if you don’t already have a ticket, see you next year.

The other part of day 1 will be the MooseCamp Unconference, the third year of this self-organizing, community from chaos event. The moose are loose and they converge to converse again! Organic and from the hip and with food this year!

Like any good participant in creations from chaos, I’m late in organizing a session — or maybe early because many sessions will be unveiled right at the event. Anyway, over the last week, I’ve been making some virtual calls to WordPress aficionados attending Northern Voice, very modest ones at that, and the result is a dynamic session where we will work together to solve our WordPress problems, WordPress and your Problems.

The plan — plan to change — is for the first half to be spent discussing problems and experiences in small groups and then us all coming together to discuss the groups’ discoveries and some of the problems that still taught us with the whole group.

Hopefully, in the small groups, you will also swap blogging stories and 411.

Some of the small groups could be solving problems related to:

Additional experts on hands:

The groups will be formed around your problems. What problem do you need help with? Post a comment here or add to the wiki page.

As I mentioned each of these aficionados is modest — some I had to trick to volunteer — and I bet you are modest too. WordPress is a rich environment, and by sharing your insights with us, you will surprise yourself with your own expertise . You will also be surprised that we have the same problems. Is there an interesting problem that you have solved and can help others solve to?

Depending on where the interest is we will reconfigure the session on the fly.

It won’t be scheduled during Blogging 101 or Photocamp. Any other sessions to avoid butting heads with? I won’t be arriving until Friday morning, because I’m still through mid-April at the Pregnancy Conference and after that I will be at the First Baby Conference for at least a few months. If there is interest some of us can get together later in the day and take a look at WordPress 2.5 which is still in development, but nearing beta quality, but I want this to be focused on solving people’s problems today.

Welcome to the Thunderdome! Bloggers and problems enter, only bloggers exit!

Thunderdome