Tag: regular expressions

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


A stupid Xcode trick, for find and replace with carriage returns

I’m really proud of this new trick, which means I’m getting down and dirty with work it’s obvious I haven’t been fly fishing in a while.

I was taking a look at a pretty complex CSS file, but the whole thing sits on a single line. There were no carriage returns, so analysis was a pain in the butt. More like impossible. I’m a bonafide moron when it comes to REGEX, but I really wanted some returns in there.


I knew each line should end with a closing bracket ( } ), so I added a \n using find and replace.


Now want to turn those \n into real breaks. I look for \n, and in the replace box I hit [Control-Q] followed by the [Return] key. Note that nothing shows up in the replace box, but XCode knows the commands are there (why I’ll never know – I’m a moron in that department too). Then I click on Replace All.


VoilĂ !

Editor’s note: This guy really needs some time on the water.

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.