Feed Only & Site Only Shortcodes

So you’re running a blog and some of your readers are following it via your RSS feed and their feed aggregators. I have to say that I’m not a fan of excerpts (summaries) in feeds, I’d rather give my feed readers the full content to save them the extra clicking trouble (it’s an option under Settings – Reading in your admin panel).

Today’s post is about having more control over the content that you provide to your feed readers. We’ll create two shortcodes which can be used throughout the post contents to specify which part has to be shown in the feed only and which one is readable from the website. We’ll call them feed-only and site-only.

Why Bother?

There are many different reasons for this kind of approach (besides, feed aggregators are different too), but I will only name a few:

  • They generally don’t have JavaScript support
  • Some might even have limited CSS and styling support
  • If you’re using syntax highlighting in your code snippets, 90% feed readers are getting it plain (derived from the above)
  • Huge images can break feed aggregators
  • Feed readers don’t have comment forms
  • Feeds are sometimes delivered by e-mail and e-mail is sometimes read from a mobile device ;)

You might be doing a giveaway on your website or sharing some premium content. Now during those times you would generally like people to actually come to your website, grab the giveaway, leave a comment and tweet or share your post. Maybe click a few of your adverts too, although ads are performing quite well (and quite expensive!) in RSS feeds too ;)

Here’s an Example

Suppose we’re doing a giveaway and we’ve got some text that goes like this:

Click here to grab your free copy of XYZ

But want your feed readers to read

Dear feed reader, we have to say that this is premium content and we’ll really appreciate if you click here to go to our website, grab the download link and leave a comment on our post!

Feed Readers vs Website Readers

And that’s where our new shortcodes will come in. You can wrap any content inside one of the two shortcodes to show it exclusively in your RSS feed or on the website. I’ll show a few more examples later on, but now let’s get to some coding.

Creating the Feed Only Shortcode

It’s up to you whether to create this as a plugin or use the functions.php file for that. I’ll go with the functions file in my theme and here’s the code:

add_shortcode( 'feed-only', 'my_feed_only_shortcode' );
function my_feed_only_shortcode( $atts, $content ) {

    if ( is_feed() )
        return apply_filters( 'the_content', $content );

    return '';
}

We’re using the add_shortcode function from the Shortcode API to register a new shortcode called feed-only. Our callback function accepts two arguments, first of which is never used. The second ($content) argument will contain whatever is in between our opening and closing short tag:

[ feed-only ] some content goes in between [/ feed-only ]

Note that when writing the shortcodes I use extra spaces to avoid WordPress actually parsing them. In reality you’ll have to strip those spaces to get it to work. Anyhow, the function internals are very simple — we’re using the is_feed conditional tag to see whether the request was for a feed or not and return the given content if it was, and an empty string otherwise.

The extra call to apply_filters ensures that any content inside the $content variable gets treated as content, meaning it’ll receive it’s proper markup, and it’ll parse any shortcodes that are inside. This means that if you’ve got a shortcode to insert a banner advert, you can use it within the feed-only shortcode :)

Creating the Site Only Shortcode

A very, very similar approach. I’d say redundant code but there’s nothing cleaner I can think of. Yes, you can introduce an attribute to one shortcode and have it handle both ways, but that will only make it more complicated to use. Here’s how our site-only shortcode looks:

add_shortcode( 'site-only', 'my_site_only_shortcode' );
function my_site_only_shortcode( $atts, $content ) {

    if ( ! is_feed() )
        return apply_filters( 'the_content', $content );

    return '';
}

Spot three differences between the two. Right, the shortcode name, the callback function, and the extra exclamation point (inequality) before the call to is_feed meaning it’ll return the content when the conditional tag returns false, i.e. we’re on the site. Rest is exactly the same as the feed only shortcode, so let’s get to some usage examples.

Usage

Starting off with the first example I’ve shown, and keep in mind my extra spaces in the code snippets that you should avoid in your content :)

[ site-only ]Click here to grab your free copy of XYZ[/ site-only ]
[ feed-only ]Dear feed reader, we have to say that this is premium content and we'll really appreciate if you click here to go to our website, grab the download link and leave a comment on our post![/ feed-only]

Now, suppose you’re posting a gallery:

[ site-only ] [ gallery ] [/ site-only ]
[ feed-only ] Galleries are viewable from our website only, click here to proceed [/ feed-only ]

And of course an advert block:

[ feed-only ] [ banner-ad ] [/ feed-only ]

Conclusion

Use this with care folks, don’t annoy your feed readers since they’re probably the only ones reading your content every day so if you make them angry they’ll unsubscribe. And rather than giving limited content to your feed readers, you might want to give that little extra to your website readers if you’re there for the traffic ;)

Enjoy your day and let us know if this was helpful and what other ways to control your feed/website readers you can think of. Use the comments section below to share your thoughts and thanks for staying tuned to our feed. Thanks!