How To Customize Feed In WordPress

WordPress provides 2 types of feed: full text and summary. The full text feed shows whole content of posts with all HTML tags, and the summary by default shows only 55 first words without any HTML tags. But, in some cases, you want something between them, like: show a predefined number of words in feed with some allowed HTML tags (images, links, bold text, etc.). In this tutorial, we’ll see how to customize feed in WordPress to achieve that.

better feed How To Customize Feed In WordPress

First, to customize feed in WordPress, we need to use these hooks: the_content for full text feed and the_excerpt_rss for summary feed. Add these lines to your functions.php file of your theme:

add_filter('the_excerpt_rss', 'dbt_custom_feed');
add_filter('the_content', 'dbt_custom_feed');

The second parameter in these lines is the name of our function, used to customize feed. Here it is (again, add to your functions.php file of your theme):

function dbt_custom_feed( $content )
{
	global $post;

	if ( ! is_feed() )
		return $content;

	// Remove all shortcodes
	$content = strip_shortcodes( $post->post_content );

	// Remove all html tags, except these
	$allowed_tags = array(
		'p'      => array(),
		'a'      => array( 'href' => array() ),
		'strong' => array(),
		'em'     => array(),
		'img'    => array( 'src' => array(), 'width' => array(), 'height' => array() ),
	);
	$content = wp_kses( $content, $allowed_tags );

	// Get only some characters
	$chars_count = 600;
	$content = wordwrap( $content, $chars_count, '[dbt]' );
	$content = explode( '[dbt]', $content );
	$content = $content[0];

	// Balance tags
	$content = balanceTags( $content, true );

	return $content;
}

In this function, we need to check if the content is displayed in feed or not. If it isn’t, then do nothing. This check is required because we use the_content hook. This hook is also used in case of display post content in homepage, single page. There’s another hook for the content of feed, it is the_content_rss, but it’s deprecated now.

The next line trips all shortcodes in post content. Note that, we use the original post content.

$content = strip_shortcodes( $post->post_content );

Then, we strip all HTML tags, except the predefined allowed tags. This is done by using a powerful WordPress built-in function: wp_kses(). This function has 3 parameters:

wp_kses( $string, $allowed_html, $allowed_protocols );
  • $string: Content to filter through kses
  • $allowed_html: List of allowed HTML elements
  • $allowed_protocols: Allow links in $string to these protocols.

List of allowed HTML elements is defined as an array. Each element of array (i.e. HTML tag) is defined also as an array with its attributes:

$allowed_tags = array(
    'p' => array(),
    'a' => array( 'href' => array() ),
    'strong' => array(),
    'em' => array(),
    'img' => array( 'src' => array(), 'width' => array(), 'height' => array() ),
);

In this example, we allow only p, strong, em, a and img tag (with some attributes of a and img). If you need more HTML tags in feed, you can change this definition.

To strip feed to predefined number of characters, we use the following code:

// get only some characters
$chars_count = 600;
$content = wordwrap( $content, $chars_count, '[dbt]' );
$content = explode( '[dbt]', $content );
$content = $content[0];

The wordwrap will wrap a string to a given number of characters using a string break (in this case, it is '[dbt]'). This function can help us not to break a long word. Then, we explode the content into an array and take the first part. The number 600 is the number of returned characters, you can change it into any number you want.

Because the process of cutting string can break the HTML tags, we need to balance them, using WordPress built-in function:

$content = balanceTags( $content, true );

The second parameter of balanceTags tells the function to force balancing HTML tags.

Now our feed isn’t full text feed though, but it looks much more prettier than summary feed. It can attract readers, and when they need full information, they can to go to original article to read. Based on this idea, you can customize your feed in many ways to make it more beautiful, such as adding some intro text in the beginning of feed content or add a footer to it.

I hope this tutorial is helpful for you when handle with feed. If you have some thoughts, feel free to share them in the comments.

18 Comments

  1. Great how to!

    I had no idea balanceTags() exist ! Thank you again !

    Reply
  2. Hello Rilwis, I’m new in this blogging field and there is lots of things i have to know, which is very important in this field. The way you present your info is much easier and more knowledgeable. It Increase my knowledge and boost my moral. Know i feel more confident and will taking advantage through this. Thanks for your marvelous info.

    Reply
  3. Thanks rilwis!

    Its great! Actually I never found such a useful topic so far! I deal with RSS feed as a blogger, but never worked deeply on it.

    Thank you for this!

    Reply
  4. Great!
    But what with the video?
    For example, embed from youtube?

    Reply
  5. Thanks for the help! How do you view this feed in a browser? Can you say what the feed URL is please?

    Reply
    • View source of your homepage, you’ll see the rss feed link. Open it in browser, you’ll see how it looks.

      Reply
  6. I sometimes see very cute RSS images. Where can I get those? Like the one you have on this page? Thanks.

    Reply
  7. I just want to inform you that i’m simply all new to blogging and definitely loved youre web site. Most likely I will bookmark your site . You certainly possess exceptional articles. Thanks for sharing with us your blog.

    Reply

Leave a Reply