• Skip to primary navigation
  • Skip to main content
  • Skip to footer

WPJohnny

WordPress Performance Guides and Reviews

  • Start a WordPress site
  • Hosting
  • Themes
  • Plugins
  • Blogging
  • Marketing

RSS output hacks – exclude categories, add spacing

WordPress development Jun 22, 2021 by Johnny 3 Comments

Some fun hacks Walter (WPAllied) did for an investing site I built.

I was building a stock investing site…that was auto-picked up by Zapier’s RSS tool, which pushed to Buffer’s auto-posting schedule, which then pushed to Twitter.

My goal was to show the following on Twitter in exactly this manner:

  • Post title
  • Post url
  • And stock symbols (e.g. $ABC, $DEF, $GHI) which I set as “tags”.
Title of my stock post https://mysite.com/posturl $ABC $DEF $GHI

But the problem was the default WordPress RSS output showed all categories and tags, and also in a format I didn’t want.

  • It showed the categories. (Again, I didn’t want to show this.)
  • It put commas between each category/tag. (Wasting space.)
  • There was no space before the tags, making them unclickable in Twitter.
Title of my stock post https://mysite.com/posturl CAT1,CAT2,TAG1,TAG2

Walter’s code fix below:

  • Declare a namespace to avoid code conflict.
  • Output RSS category element to show categories without commas and with a space between each one.
  • Disable categories from showing in feed output. (Now shows only tags.)
  • Put this snippet in your theme functions.php file.
/**
 * Add <yoursite> namespace to RSS2.
 */
add_action( 'rss2_ns', function() {
	echo 'xmlns:yoursite="http://yoursite.com/ns/yoursite#"';
});

/**
 * Show post categories/tags in feeds with a single string space separated, and no comma.
 * 
 * Disable Categories from showing in feed output.
 */
add_action('rss2_item', function() {
//	$categories = get_the_category();
	$tags       = get_the_tags();
	$the_list   = '';
	$cat_names  = array();

//	if ( ! empty( $categories ) ) {
//		foreach ( (array) $categories as $category ) {
//			$cat_names[] = sanitize_term_field( 'name', $category->name, $category->term_id, 'category', 'rss' );
//		}
//	}

	if ( ! empty( $tags ) ) {
		foreach ( (array) $tags as $tag ) {
			$cat_names[] = sanitize_term_field( 'name', $tag->name, $tag->term_id, 'post_tag', 'rss' );
		}
	}

	$cat_names = array_unique( $cat_names );
	$cat_name  = implode( ' ', $cat_names );

	$the_list .= "\t\t<yoursite:categories><![CDATA[" . html_entity_decode( $cat_name, ENT_COMPAT, get_option( 'blog_charset' ) ) . "]]></yoursite:categories>\n";

	echo $the_list;

I hope you enjoyed! It was a fun little hack and did the job perfectly for what we wanted!

Share this post:

Share on FacebookShare on X (Twitter)Share on LinkedInShare on WhatsAppShare on EmailShare on SMS

Read all my posts on WordPress development

About Johnny

Right on the edge of WordPress development! 10+ years of WordPress design, development, hosting, speed optimization, product advisor, marketing, monetization. I do all that.

More WordPress Guides

Genesis vs Astra – WordPress Theme Review 2019

Is it worth blogging on 3rd-party platforms? (Medium, Tumblr, etc)

Linux Server Commands CHEATSHEET (for newb admins)

Best Microphones for Online Streaming or Podcasting

Your Job is ALWAYS to Inspire

WordPress SEO Tips for 2020

Reader Interactions

3 Comments

  1. Ryan McCain

    June 23, 2021 at 12:45 pm

    rockin

    Reply
  2. Dan

    July 13, 2022 at 11:02 pm

    Speaking of RSS, why dont you add rss to your blog? It would be so much easier to follow you.

    Reply
    • Johnny

      July 14, 2022 at 9:06 am

      I already have it (default with WordPress). Are you asking for a specific RSS link button?

      Reply

Leave a Reply to Johnny Cancel reply

Your email address will not be published. Required fields are marked *

Footer

More links

  • Gadget reviews
  • Try my free WPJ plugins
  • Join the WPJ FB Group
  • WPJ YouTube & newsletter
  • Become a WPJ Affiliate

Popular Reviews

  • Best WordPress Hosting
  • Best WordPress Themes
  • Best WordPress Plugins
  • Best WordPress Cache Plugins

Services

  • Speed optimization
  • Speed optimization courses
  • WordPress hosting
  • Hire me or other experts
  • Client login

About Johnny

10+ years of WordPress design, development, hosting, speed optimization, marketing.
Contact me.

newsletter block

Copyright 2025 | WordPress guides by Johnny Nguyen

Click to Copy