avatar Deluxe Blog Tips About Projects

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.


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.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Comments