Blog
Red-faced coder adds support for feeds and trackbacks in custom permalinks
Note: This is a continuation and correction of my previous post. Please be sure to read that to get the full context.
Yesterday I felt guilty for spending too much time writing code, today I feel embarrassed for not spending enough. After I implemented my new custom permalink mechanism, I thought I’d share that with the world, or at least the readers of the WordPress support
forums, in the hopes that it might be useful to someone else. Well, soon after posting my contribution, I realized I had neglected to take care of RSS feed requests for categories, and feed and trackback links for individual postings. Arghhhh!
And to make it worse, the hit counter on my laptop was moving, meaning people were reading the post – and it was wrong! So much for going to bed – I gotta fix this now!
At least the category feed fix was simple enough: create a new /feed/ subfolder under each category folder, each with the same index.php file, with this code:<?php
$path = dirname(dirname($_SERVER['PHP_SELF']));
$position = strrpos($path,'/') + 1;
$thisDir = substr($path,$position);
header("Location: ../../../redirect.php?cat=$thisDir&feed=rss2"); exit;
?>
The feed and trackback URLs for individual posts were more problematic, mainly because I had set up permalinks with the structure /name-of-post.php (for maximum SEO’ness). For individual post feeds and trackbacks, WordPress appends the folder name to the permalink URL, like so: /name-of-post.php/feed/. What this meant was that I would have to convert all my permalink files to permalink folders, so they could have sub folders. And since I had to do that manually, I might as well get rid of the .php in the folder name, since that would result in a weird-looking URL that might freak some people (or robots) out. This had the further unfortunate effect of rendering the permalink I posted on the forum obsolete. Let that be a lesson for you kids – don’t post in haste.
Curiously, the trackback link now works exactly as it did when I had the default permalinks, which is to say, not at all. At this point, I assume this is a completely different issue.
Ok, each permalink folder now contains two sub-folders: /feed/ and /trackback/. The index.php in /feed/ looks like this: <?php
$path = dirname(dirname($_SERVER['PHP_SELF']));
$position = strrpos($path,'/') + 1;
$thisDir = substr($path,$position);
header("Location: ../../redirect.php?p=$thisDir&feed=rss2"); exit;
?>
Note that this and the category feed index.php code converts all sub-feeds to the rss2 format, regardless of what the requestor asked for. This will probably break some feed readers, but I was already spending way more time on this than I should, and the main blog feed is unaffected. If I really wanted to fix this, I would probably replicate some of the functionality of wp-feed.php into the /feed/index.php files.
The index.php in /trackback/ contains this code: <?php
$path = dirname(dirname($_SERVER['PHP_SELF']));
$position = strrpos($path,'/') + 1;
$thisDir = substr($path,$position);
header("Location: ../../redirect.php?p=$thisDir&trackback=true"); exit;
?>
Finally, the redirect.php file had to be updated to support this new functionality. This is what it looks like now: <?php
require_once('wp-config.php');
$URLquery = "";
if (array_key_exists("trackback",$_GET)) {
$postName = $_GET['p'];
$postID = get_object_vars($wpdb->get_row("SELECT ID FROM $wpdb->posts WHERE post_name = '$postName' LIMIT 1"));
$postID = $postID['ID'];
header("Location: wp-trackback.php?p=$postID");
exit;
}
if (array_key_exists("cat",$_GET)) {
$catName = $_GET['cat'];
$catID = get_object_vars($wpdb->get_row("SELECT cat_ID FROM $wpdb->categories WHERE cat_name = '$catName' LIMIT 1"));
$catID = $catID['cat_ID'];
$URLquery = "?cat=$catID";
}
if (array_key_exists("m",$_GET)) {
$month = $_GET['m'];
$URLquery = "?m=$month";
}
if (array_key_exists("p",$_GET)) {
$postName = $_GET['p'];
$postID = get_object_vars($wpdb->get_row("SELECT ID FROM $wpdb->posts WHERE post_name = '$postName' LIMIT 1"));
$postID = $postID['ID'];
$URLquery = "?p=$postID";
}
if (array_key_exists("feed",$_GET)) {
$feed = $_GET['feed'];
$URLquery .= "&feed=$feed";
}
header("Location: index.php$URLquery");
?>
Am I happy with this solution? Not entirely - RSS compliance is not 100%, and I’ve got a mess of folders and sub folders in my blog file structure. Indeed, one response to my initial forum posting
calls my solution a kludge, and I have to agree. I am hoping someone steps up with a better solution, other than “switch hosts”. I’m all ears !
No Comments yet »
RSS feed for comments on this post.
Trackback URI for this post:
http://austinmash.com/blog/red-faced-coder-adds-support-for-feeds-and-trackbacks-in-custom-permalinks/trackback/
Leave a comment
You must be logged in to post a comment.





