I’ve been wandering around on the web for the last couple of hours wondering if there’s a way to allow users to build their own RSS feed by selecting categories within WordPress, that could then be subscribed to by email. Seems to present two problems:
- Allowing people to build a personalized feed from categories.
- Enabling email subscription.
Any thoughts on how best to proceed with either?
This is a really cool idea.
I don’t think part 2 should be handled inside WordPress: there are plenty of RSS to email providers. They’re going to be way better at that than a plugin (or theme) is likely to be.
BUT we can create RSS feeds.
Step one: set up a class to wrap everything up.
There are a few class constants and variables here — we’ll use them later. Just a singleton pattern.
Step two: add a field to the user profile pages (and save it)
You’ll need to hook into
show_user_profile
andedit_user_profile
to do this. Spit out a nonce, a label, and the field.show_user_profile
fires when users view their profile in the admin area.edit_user_profile
fires when they edit another’s profile — this is how your admin user will go in an edit user’s categories.That also introduces our first two helper methods:
get_user_terms
, a simple wraper aroundget_user_meta
with a call toapply_filters
— let others modify things if they want!get_terms
a wrapper aroundget_terms
with a call toapply_filters
.Both of these are just convenience things. They also provide ways for other plugins/themes to hook in and modify things.
To save the fields, hook into
personal_options_update
(fires when user save their own profile) andedit_user_profile_update
(fires when saving another user’s profile).Step three: provide a feed
Since this is very much a custom feed, we don’t want to hijack something like author feeds to get this done (though that’s an option!). Instead let’s add a rewrite:
yoursite.com/user-feed/{{user_id}}
will render the personalized user feed.To add the rewrite we need to hook into
init
and useadd_rewrite_rule
. Since this uses a custom query variable to detect when we’re on a personalized user feed, we also need to hook intoquery_vars
and our our custom variable so WordPress doesn’t ignore it.To actually render the feed, we’ll hook into
template_redirect
, look for our custom query var (bailing if we don’t find it), and hijack the global$wp_query
with a personalized version.I also hooked into
wp_title_rss
to modify the RSS title, which was a bit weird: it grabbed the first category and displayed the feed title as if looking at a single category.To actually render the feed we rely on
wp-includes/feed-rss2.php
. You could replace this with something more custom, but why not be lazy?There’s also a third helper method here:
get_user_query
. Same idea as the helpers above — abstract away some reusable functionality and provide hooks.Here is all of the above as a plugin. The plugin (and subsequently, this answer) requires PHP 5.3+ due to the use of anonymous functions.
I do this using the regular WordPress Category Feeds and MailChimp to provide my email subscribers the option of receiving new posts only for categories they’re interested in receiving.
Within MailChimp, you create a Group for each WordPress category and then on your email subscription form you allow your subscribers to select the groups (i.e., categories) they’re interested in subscribing to (a set of checkboxes is probably easiest). When they subscribe, their selections will be passed along and they will be put into those groups on MailChimp.
Then on MailChimp you create an RSS Campaign for each category using the Category Feed and specify in the campaign settings to only send new posts to a segment of your subscribers (the segment that has selected the group corresponding to that category).
The simplest would be to add a series of two very short (mu-)plugins. This also adds routes for
page/2
, etc.:http://example.com/u/%author%
http://example.com/p/%postname%
WordPress already supplies RSS feeds for each category, this is the codex article explaining how they’re structured:
http://codex.wordpress.org/WordPress_Feeds#Categories_and_Tags
To get email subscription working I usually set up Feedburner with email subscription enabled (after claiming a feed, go to Publicize > Email subscriptions). That would require you to take your category feeds and set each one up in Feedburner, then add those links to your site in appropriate places. If you’re dealing with a ton of categories that might be quite a bit of work. Hopefully other people here will have suggestions.
Best of luck!