Change a Post’s RSS Permalink Using Custom Fields

12 Comments

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_box function 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!

12 thoughts on “Change a Post’s RSS Permalink Using Custom Fields

  1. 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.

  2. 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.

  3. @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 $url for 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.

    • Dan, please excuse my ignorance as I’m not very well versed in php!

      I’m trying to implement your code in my theme’s function page, but it doesn’t seem to do what I was hoping for – namely, change the permalink for a post IF a custom field called ‘postURL’ exists. I think that’s what you were trying to explain to @Sumesh, but somehow I don’t understand to how implement what you’ve described as:

      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 $url for the link, and after } else { it just links to the regular permalink.

      What would the full code look like including this addition?

  4. Pingback: Weekend Links - Dec 26, 2008 « OMNINOGGIN

  5. Pingback: Weekend Roundup #30 » JaypeeOnline // Blogging News & Reviews

  6. 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.

  7. Pingback: 100+ Massive Wordpress Tutorial Collection | tripwire magazine

  8. 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.

  9. Hi, I’ve integrated this code into my functions.php file, ’cause I’m actually working on a “press review” widget based on a custom post and with lots of external links. But it doesn’t work. Is there anything I’ve done wrong ?

Comments are closed.