0

We're sending RSS emails out to our list and would like to include the categories and tags.

But when we add them, due to the way WordPress delivers them, they just end up in one big long string/stacked end to end, one after the other, like this:

Digital ContentSocial MediaFacebookZuckerberg

I'm trying to find a way to get rid of the individual elements and just call my main, comma separated, category list, inside a single element, instead.

So instead of:

<category><![CDATA[Digital Content]]></category>
<category><![CDATA[Social Media]]></category>
<category><![CDATA[Facebook]]></category>
<category><![CDATA[Zuckerberg]]></category>

We'd have something like:

<category><![CDATA[Digital Content, Social Media, Facebook, Zuckerberg]]></category>

I'm looking at the_category_rss(), which seems designed for the purpose, but no matter what code I try I can't get it to override the individual s.

function rtt_rss_cats($the_list,$type) {
    $the_list = "PUT SOMETHING ELSE HERE";
    return $the_list;
}
add_filter('the_category_rss', 'rtt_rss_cats', 10, 1);

The main function appears to be get_the_category_rss(), and includes the filter. I just can't get it to work/override this:

function get_the_category_rss( $type = null ) {
        if ( empty( $type ) ) {
                $type = get_default_feed();
        }
        $categories = get_the_category();
        $tags       = get_the_tags();
        $the_list   = '';
        $cat_names  = array();

        $filter = 'rss';
        if ( 'atom' == $type ) {
                $filter = 'raw';
        }

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

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

        $cat_names = array_unique( $cat_names );
        foreach ( $cat_names as $cat_name ) {
                if ( 'rdf' == $type ) {
                        $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
                } elseif ( 'atom' == $type ) {
                        $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
                } else {
                        $the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option( 'blog_charset' ) ) . "]]></category>\n";
                }
        }
        /**
         * Filters all of the post categories for display in a feed.
         *
         * @since 1.2.0
         *
         * @param string $the_list All of the RSS post categories.
         * @param string $type     Type of feed. Possible values include 'rss2', 'atom'.
         *                         Default 'rss2'.
         */
   return apply_filters( 'the_category_rss', $the_list, $type );
}

1 Answer 1

1

Try to add the following code in functions.php of your theme and it should do the job.

function dcg_tidy_rss_categories( $the_list ) {
    $categories = get_the_category();
    $tags = get_the_tags();

    if ( ! empty( $categories ) ) {
        foreach ($categories as $category) {
            // remove the following line if you don't want Categories to appear
            $the_list = str_replace("<category><![CDATA[{$category->name}]]></category>", "$category->name, ", $the_list);
        }
    }

    if ( ! empty( $tags ) ) {
        foreach ($tags as $tag) {
            // remove the following line if you don't want Tags to appear
            $the_list = str_replace("<category><![CDATA[{$tag->name}]]></category>", "$tag->name, ", $the_list);
        }
    }

    return "<category><![CDATA[" . preg_replace('/\s+/S', " ", trim( rtrim( $the_list, ", \t\n" ) ) ) . "]]></category>";
}
add_filter( 'the_category_rss', 'dcg_tidy_rss_categories' );
1
  • 1
    Outstanding! That's absolutely perfect! Thank you, Dipak! :D
    – jongoeson
    Aug 10, 2019 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.