If you’re subscribed to John Gruber’s Daring Fireball RSS feed, you’ve probably noticed that most of his posts are links to other sites. Instead of linking back to his site first and then to the site he’s referring to, you’re simply taken straight to the topic website from the RSS post. In other words, his RSS permalinks are modified to link to the source site instead of his blog, saving an extra click and page load for his readers. John’s site runs on Movable Type, but this function can be easily done with WordPress as well. Read on to find out how to write this simple function. Thanks to Ryan McCue for the code for this tutorial.
For the sake of simplicity we are going to create this function within a theme, so open the file functions.php in your active theme, or create one if that file doesn’t exist. Add the opening and closing PHP tags and create a new function named mp_permalink().
<?php
function mp_permalink($permalink) {
}
?>
To determine what the new permalink will be, we’re going to use a custom field. To do this, we’ll have to fetch post data first, so inside the function, add global $wp_query;. Adding this variable will have the function fetch post data when creating or editing a post.
Next, we’re going to check to see if the custom field for the URL exists. If it does, the RSS permalink will be changed. Otherwise, it’ll stay the same. Simple, right?
To check for this custom field, we use an if() statement:
if($url = get_post_meta($wp_query->post->ID, 'url', true)) {
return $url;
}
If you’re familiar with how to retrieve custom field data from a post then this will make sense to you. If a value exists for the custom field ‘url’ in the post, the field’s value, which is the URL, is returned.
To give the function a value, we add return $permalink; to the end of the function. Now, when the function is run, the URL from the ‘url’ custom field will be returned if it exists. Here’s what we have so far:
<?php
function mp_permalink($permalink) {
if($url = get_post_meta($wp_query->post->ID, 'url', true)) {
return $url;
}
return $permalink;
}
?>
Finally, to change the RSS article’s permalink, we’re going to use a WordPress filter. WordPress provides dozens of these filters to hook into various processes that run “behind the scenes”. This particular filter, ‘the_permalink_rss‘, allows you to modify the RSS article’s permalink.
add_filter('the_permalink_rss', 'mp_permalink');
The first value of add_filter is the filter we are referring to, and the second value is the function that will be run with this filter. Here’s the final code:
<?php
function mp_permalink($permalink) {
global $wp_query;
if($url = get_post_meta($wp_query->post->ID, 'url', true)) {
return $url;
}
return $permalink;
}
add_filter('the_permalink_rss', 'mp_permalink');
?>
Further Development
There are lots of ways you could extend this function even further.
- Using the new
add_meta_boxfunction you could skip custom fields and add a special box just for the permalink. - Explore all of the available filters to learn how to modify all kinds of information in the WordPress database.
- If you run a mostly linked list blog (like Daring Fireball or WPLover) you could use this function to take away that extra click for your RSS subscribers to direct them straight to the source site.
Hopefully you were able to learn something from this tutorial. Happy holidays!
Post Revisions:
- 16 August, 2010 @ 11:03 [Current Revision] by dan philibin
- 9 August, 2010 @ 6:23 by Ryan Imel
- 5 August, 2010 @ 4:51 by Ryan Imel
- 22 December, 2008 @ 8:00 by Ryan Imel
Posted December 22, 2008
Sumesh said:
I’ve bugged a lot of people for a tutorial of this kind, but never got one. Beats me why I didn’t ask WPcandy, though
I’m subscribed to Daring Fireball, and one of the reasons I like DF, WPLover (which I read for the first time yesterday) and Geekaholic is because of their links to hot topics. I’ll be sure to replicate the linked list on my blog too – thanks again for this tut, Dan.
on December 22, 2008 at 12:47 pm
Patrick Algrim said:
Very awesome tutorial. Very simple and super effective. This is definitely going to be a resource that others will be coming back to time and time again!
on December 22, 2008 at 9:11 pm
Sumesh said:
I tested this, but didn’t get it to work. Perhaps I made a mistake.
On a side note, what would site visitors do? If you do not link to the source in post (and do it through custom field only), we need a hack to change the permalink for the posts too. I hope you’ll have a tutorial on that sometime later. Until then, I’m holding off deployment on my blog.
on December 24, 2008 at 6:28 am
Dan Philibin said:
@Sumesh it’s actually quite simple to change the permalink, too – I probably should have covered that in the tutorial as well.
Under the line
add_filter('the_permalink_rss', 'mp_permalink');, just add another filter to change the post’s permalink:add_filter('post_link', 'mp_permalink');However I wouldn’t recommend changing the actual permalink to your post. Then there’s no way to reference the post after it’s gone from your homepage. In the theme, instead of linking the posts’s title to its permalink, just link it to the custom field URL.
post->ID, 'url', true);if($url) { // if URL custom field exists, use that URL: ?>
Our code cleaner-upper doesn’t seem to be working with comments. Basically inside this if() statement you echo
$urlfor the link, and after} else {it just links to the regular permalink.This would go in your theme’s file. Hopefully this helps. Let me know if anything isn’t clear.
on December 24, 2008 at 10:46 am
Amos said:
Appreciate this write-up Dan. Very helpful. One question I have for you. After setting all this code up it appears to work great on my site/rss feed. However, I’d feel more comfortable using it if I could include a permalink back to my site in the RSS article.
As it is now the title links to the source, but there’s no way for the reader to jump to the respective article on my site, when consumed in an RSS reader. So I guess visually something like this for the RSS article:
-TITLE (link to source)
-CONTENT
-PERMALINK
Where the permalink is either the word or a symbol. Believe Mr. Gruber has it set up this way. Hope this makes sense. I’d try to figure it out myself, but fear I’d totally destroy my site in the process.
Again, much thanks for sharing this information.
on March 6, 2009 at 7:25 am
Norbert said:
Hi now that it’s clear how to hand over a custom field value to the rss (thank you very much for this tut.) I would be very curious how to read out any custom field value with a (eg.) simplepie loop.
Thank you in advance.
on February 17, 2010 at 5:31 am