Tag: tags

Rewriting tag underscores to hyphens for WordPress 3.1

When I first moved existing content to this domain, I had a pile of tags coming from Movable Type with underscores (“_”) in them. Upon getting them into the new database, I changed the underscores to hyphens using a SQL script, but then had to worry about redirecting old links to new. I wound up with a very long .htaccess file full of 301 directives that looked like this:

Redirect 301 /tag/tags_with_underscores/ https://michaelgracie.com/tag/tags-with-underscores/

With the upgrade to WordPress 3.1, I started having problems with URL rewrites – the culprit wound up being the Advanced Permalinks plugin. That plugin had been used as a patch, allowing pretty permalinks to function alongside some stray special characters such as periods (“.”). Once I disabled it I was forced to clean up those special characters, and it then dawned on me that this list of redirects was WAY too long. So I set out to conjure another solution for the original underscore issue. After significant research, followed by too much trial and error, this is what I came up with…

If you are using clean URLs, WordPress has inserted this chunk of code into your site’s .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

Inserting this additional code in between “RewriteBase /” and “RewriteRule ^index\.php$ - [L]” will change the underscores in individual tags to hyphens:

#begin permanent tag fix
RewriteRule ^tag/([^_]*)_([^_]*)_([^_]*)_([^_]*)$ tag/$1-$2-$3-$4 [L,R=301]
RewriteRule ^tag/([^_]*)_([^_]*)_([^_]*)$ tag/$1-$2-$3 [L,R=301]
RewriteRule ^tag/([^_]*)_([^_]*)$ tag/$1-$2 [L,R=301]
#end permanent tag fix


Why I’m just a little code monkey

Sometimes you’ve just gotta leave it to the pros

I had this little problem with the WordPress Codex a while back – I wanted to display a link to feeds for my tags on the tag pages. WordPress is pretty good about presenting the data – I used ucwords(single_tag_title("", false)) to properly display the tag names at the top of the page (see here), but the codex didn’t have a function for returning the link part (i.e. /fly-fishing/) without a ‘tag id’ and the additional page numbering was getting in the way too. So I hacked together this work-around:

if (preg_match("/\/page\//",$_SERVER['REQUEST_URI'])) {
$feedtarget = $_SERVER['REQUEST_URI'];
$feedreplace = array ('|/page/2|','|/page/3|','|/page/4|','|/page/5|','|/page/6|','|
$feedchange = array ('','','','','','','','','','','','','','');
$feedfixed = preg_replace($feedreplace,$feedchange,$feedtarget);
$feedmatch = $feedfixed.'feed/';
} else {
$feedmatch = $_SERVER['REQUEST_URI'].'feed/';

And was stuffing that $feedmatch bit into the link by echoing it after bloginfo(‘url’).

What I can manage to get out the door usually works, even if it is less than elegant. Ok, way less than elegant (and limited on the page number end too). I could use the excuse that I was in a rush, but the fact is I spent at least an hour getting it to work right, and once it did I felt the hero in my own mind.

After re-arranging my categories a bit, I thought about extending the ‘patch’ to those and posting the work for others. It was then I realized that was some craptastic code like you read about. So I ask a friend if he could help me clean up the array portion. He does one better, by reducing my entire bunk to one line:

$feedmatch = preg_replace('/\/page\/[0-9]+/', '', $_SERVER['REQUEST_URI'], 1).'feed/';

Proving once and for all that Regular Expressions should be basic coursework for accounting grads (or at least qualified CPE). And I should stick to tumbling numbers and fly fishing.

WordPress and FeedBurner FeedSmith: Getting to your category and tag feeds

Last time I touched on WordPress and Feedburner, the topic was how to tweak the Feedsmith plug-in so you could get access to your raw feed with Yahoo! Pipes. This time, I’m making some alterations to the plug-in so you gain access to your raw category and tag related feeds without those requests getting redirected back to your blog’s main Feedburner feed.

For most folks using the Feedburner redirect plug-in, getting access to WordPress’s category and tag feeds seems like a nightmare. I searched and searched myself, and all I found was numerous iterations of .htaccess file solutions. None of them worked for me, so I decided to go back to the Feedburner_FeedSmith_Plugin.php file and tool around. This turned out to be a quick fix, with one caveat – I don’t care to burn additional feeds for each of my categories, and this change won’t work for you if that’s your intention. In my case the blog categories are organized by “origin” of the post, not subject matter – what I really wanted was access to the tag feeds, which do relate to the subject of the post at hand. Here’s how to do it…

First, look to the bottom of the Feedsmith plug-in file (located under /wp-content/plugins/feedburner_feedsmith_plugin_2.2) for this:

if (!preg_match("/feedburner|feedvalidator/i", $_SERVER['HTTP_USER_AGENT'])) {
add_action('template_redirect', 'ol_feed_redirect');

That “!preg_match” bit tells WordPress to ignore redirect requests from Feedburner or FeedValidator so those services don’t wind up in an endless loop when trying to grab your raw feed. And it’s this same section of the plug-in code that pushes requests for category and tag feeds back to Feedburner for so many agitated users. We change that block of code to this (changes emphasized):

if (!preg_match("/feedburner|feedvalidator/i", $_SERVER['HTTP_USER_AGENT']) && !preg_match("/category|tag/i", $_SERVER['REQUEST_URI'])) {
add_action('template_redirect', 'ol_feed_redirect');

That additional “!preg_match” string tells WordPress to ignore redirects to Feedburner when the requested URL contains “category” or “tag”, but there is still one more thing you’ve got to do. In this blog’s case, my category base is “origin” – so in place of “category” I use the word “origin”; I still use “tag” for my tag base, so that stays the same. You set your category and tag bases in the WordPress admin panel under Settings..Permalinks – look for this:


If your optional permalinks are set, change the code snippet above to reflect the same (/’s excluded) and save – otherwise set them as desired first then do the same. Now you should have raw category and tag feeds that work.

Two of the most popular tags on this blog are “Fly Fishing” and “Fannie Mae” – and here are their feeds:

  • Fly Fishing
  • Fannie Mae
  • Try it for yourself!

    Good Content versus Tricked Distribution

    A colleague and I have been running a jointed, intermittent conversation on the merits of good content. He spends a lot of time expounding on the issues of poor or untruthful press coverage, the sheer enormity of blogs out there, and questions why nobody has done it quite right. I have to agree – Thoughtmarket smells a bit putrid at times, but at least I know it.

    Then, early last week he directed me to Remove Forebrain and Serve: Tag Clouds II, from Jeffrey Zeldman, and it struck a cord.