WordPress Feed Modifications
Posted on Friday, September 04, 2009
Awhile back I modified the titles in my RSS feeds to display "link:" or "tweet:" to my linklog/twitter posts on this blog. A bit later I decided to add Creative Commons license info in my feed as well. I know there are several existing plugins that do that, but I already had a plugin of my own that did some feed modification so I was familiar with the relevant WP hooks, so I went ahead and added this to my existing feed plugin myself.
Today I wanted to add yet another feed modification: add a link back to the original article to the end of the post content on each of my posts. A common reason to want this is to prevent (or at least inconvenience) scrapers. I wish I were that popular. My problem's much simpler: I wanted to put my blog posts into Facebook, as sadly Facebook is apparently a bigger draw for most of my friends than my blog is. (I know, I don't get it either.) For awhile I was using the Wordbook plugin, but it screwed up one too many times on me, so I opted to just let Facebook's Notes import my RSS feed, which is super easy. However, Facebook Notes don't link back to the original source at all. Just looking at my Facebook Notes, there's no way to know it originally came from my blog. I obviously don't care whether my friends read my posts on my blog or in Facebook, but I'd still like them to know about it. Plus, unlike Facebook, I actually control my blog. In five or ten years, I'll still control my blog. So whatever I write, no matter how silly & meaningless to anyone else, the master copy belongs on my blog. So I wanted to add a simple link to the end of the posts that would show up in the Notes.
Again, I know for a fact there are several plugins that do just this (likely with way more flair), but just adding a plain ol' link is simple enough to do, so I took the DIY route again.
So for anyone interested in either of these modifications that prefers a do-it-yourself approach to plugins, I thought I'd post both of these very simple modifications. If you're just learning how to write WordPress plugins, these actually make pretty good examples as well precisely because they're so simple. So just create your plugin file and then add one or both snippets of code below. (Or just go to wordpress.org and download the existing plugins that are probably better supported by their authors and have way more options...)
Append a link to your RSS content
This will add a link at the end of your post that says "(Originally published at http://yourblog.com)" and links to the permalink for the post. Here's the code:
Notice the comment about the bug where the_content_rss isn't applied to the content in RSS 2.0 & Atom feeds. This is slated to be fixed in 2.9, so if that happens you could just keep the add_filter('the_content_rss', 'append_to_feed') and delete everything else in run_append_filter().
Add a Creative Commons license to your feed
I chose the by-nc-sa license, but you can use whatever you want by adjusting the $feed_license array. You'll definitely want to change $feed_license['years'] as well (unless you happened to have started your blog in 2003 like me). The code is long, but simple and repetitive. All the formats have their own little twists, which is a huge pain in the ass. Anyway, here's the code: