Last tended on 12 May, 2020 (first created 10 May, 2020)

,

I am trying to add an additional RSS feed, using a different template, to my blog. Most documentation on WordPress.org seems to be aimed at replacing an existing feed, or alter its contents.

The additional feed is meant to fulfill Peter’s request to have a comments feed that shows only real comments, not all the likes, reposts etc I also receive through WebMentions. It is a very logical request, but I also want to keep the original comments RSS feed that includes all interaction with content on this site. So I need to add a new RSS feed.

WordPress.org doesn’t really contain any complete step-by-step explanations for anything, including for adding customised RSS feeds. It also doesn’t have any info on excluding pingbacks from comment feeds, which would be similar to what I’m trying to do. I tried stuff based on snippets of documentation I did find, but couldn’t put those snippets together into a coherent path to a solution.

Then I found a guide at WPBeginner dot com (WP beginner, that’s me, even though I’ve been a WP user and tinkerer since 2004) It’s from 2016 so maybe not concurrent with today’s version of WP. Following their explanation, I added the following to my child theme’s functions.php:

add_action('init', 'extracommentsRSS');
function extracommentsRSS(){
add_feed('commentsdupe', 'extracommentsrssFunc');
}

function extracommentsrssFunc() {
get_template_part('rss2', 'commentsdupe');
}

The first part adds the new feed to WP, the second function tells it where to look for the template for the feed. That template, named rss2-commentsdupe.php, is located within the child-theme folder.

The URL for the feed now works, however it first prompted a download of an empty file due to the template file having a wrong name. With that name corrected (based on Jan’s comments below), it does load the template correctly.

The template itself still has issues (meaning it doesn’t work as intended at all): the while has_comments() loop provides no results, as it does not get passed any variables. Because the get_template_part is not provided with those variables before getting called. Jan suggested adding the query to the template. Did that, but now struggle to find a way to lift the comments from the query result.

The query below gets the latest 20 approved reactions on my site of the type ‘comment’ (as opposed to e.g. pingback or webmention):

$query = new WP_Comment_Query( array( 'status' => 'approve', 'type' => 'comment', 'number' => 20, 'orderby' => 'commentdate_date' ) );

The result if I print it as arrays, contains 7 elements (the query, the query arguments, default query arguments (?), an empty one, an array with the 20 requested WP comment objects, and two more empty ones).
The query works as intended, but now I do not know how a) to access the 5th element, and b) handle the WP comment objects within it. What seems to be intended functions for it (have_comments(), the_comment()) don’t seem to work.

The below code at least does not work. If I test it like this, it never enters the while loop, so never echoes ‘BOE’

while ( $query->have_comments() ) :
echo 'BOE';
$query->the_comment();
$comment_post = get_post( $comment->comment_post_ID );
$GLOBALS['post'] = $comment_post;
?> etc.

After some more suggestions by Jan Boddez, ao that have_comments() doesn’t work for wp_comment_query, I then established that get_comments to query the comments, and then a foreach to loop through them works the way I need it to.

$query = get_comments( array( 'status' => 'approve', 'type' => 'comment', 'number' => 20, 'orderby' => 'commentdate_date' ) );

foreach ($query as $comment):
$comment_post = get_post( $comment->comment_post_ID );
$GLOBALS['post'] = $comment_post;

Once I had that the original feed template could be fully re-used, and now validates:

[Valid RSS]

11 reactions on “How to Add a Custom RSS Feed to WordPress

,

  1. @ton Deze comment is alvast ook op je nieuwe feed van toepassing: https://developer.wordpress.org/reference/functions/add_feed/#comment-2086 (je moet m.a.w. met die `header` browsers even vertellen welk type bestand ze mogen verwachten). Ook zou ik even proberen om i.p.v. meteen die template aan te roepen, even gewoon ‘Hello world!’ te echoën (of ‘aye!’, zoals in het voorbeeld). Als dat werkt, scheelt er dus toch nog wat met je template. (Uit het artikel op WPBeginner maak ik trouwens op dat die `rss2-commentsdupe.php` zou moeten heten.)
    add_feed() | Function | WordPress Developer Resources

  2. @ton Dus met `foreach` elk array-element aflopen. Kun je denk ik wel niet zonder meer de functies uit de standaard-template overnemen, of toch niet allemaal, maar je kunt natuurlijk wel steeds (cf. `$comment->comment_content`) de publieke comment-properties aanroepen. (De meeste template-functies werken trouwens waarschijnlijk wél als je er steeds het comment ID aan meegeeft. Zou je even moeten nagaan.)

  3. @ton Voor de volledigheid: WP_Comment_Query doet inderdaad niet aan `have_comments()` en verschilt op dat vlak dus van post queries. Wel kent het een methode `query` die, zoals in het voorbeeld, een array retourneert. Je kunt, zo blijkt, ook gewoon `get_comments()` gebruiken, die maakt dan een query-object voor je, en retourneert dezelfde array.

  4. @ton M’n eerdere uitleg klopte dus niet helemaal, waarvoor excuses. Ik had gehoopt functies als `comment_text()` gewoon te kunnen gebruiken. Daar ga je nu dus overal `comment_text( $comment->comment_ID )` van moeten maken. Of je moet aan het begin van elke iteratie de globale comment var instellen: `$GLOBALS[‘comment’] = $comment`. Da’s misschien nog het gemakkelijkst.

  5. @ton Ha, super! (Eerlijk gezegd, Mastodon is misschien niet het ideale medium om zoiets te bespreken. Ik bedoel, geef me een lege testsite en ik heb het waarschijnlijk binnen ‘t half uur in orde gebracht. Al wil ik je natuurlijk zelf ook wat plezier gunnen.)

  6. In response to Peter’s earlier request I have created a new RSS feed that contains only comments on postings, not other types of reactions such as likes, mentions, and ping- or trackbacks. It was a bit of a puzzle to get it all working, having me dive down the rabbit hole leading to the maze that is the WordPress documentation. With some suggestions from Jan Boddez, I now have a result. The new feed is listed on the right hand side. Subscribe to it if you care to follow conversations on this blog. The feed with all interactions, so including likes etc., is also available.
    I documented how I created the feed over in the wiki.

  7. Over 9 weeks at home, there was a change of pace as daycare has re-opened. While welcome it also took some getting used to, having time to focus all of a sudden, and less interruptions in the home. It did not immediately transform into productivity, it also translated in taking some time to do my own thing, such as tinkering with a bit of code. It felt like a very unproductive week, but mostly as I measured it against the lack of progress of a single project that I should be focused on more than I was.
    This week I

    Coded up a new rss feed for comments on this site
    Kept up keeping daily logs, for the third week in a row
    Made a ‘kanban’ style board for a project nearing a deadline, to get an overview of its various components and quircks. Used the new NextCloud Deck app for it, and it works well. Made working with a colleague on this way easier, cutting back on coordination time.
    Worked on the EU High Value Data study
    Celebrated my 50th birthday. E themed it ‘play (more)’ and gave me both a book about Play, and a Lego Boost robot to play with. I had a visit from my sister and brother in law who live nearby, and E’s brother. At the request of Y we had take-away pancakes for dinner. The evening we did a Zoom Trivial Pursuit game (with questions about me added) with dear friends in Switzerland. But the program then stretched out into the rest of the week. There was a ‘pubquiz’ on Wednesday with E’s entire family with questions about me and my birth year 1970. There was a ‘virtual escape room’ on Thursday with my colleagues, and an app-mediated scavenger hunt through our neighbourhood with my dear fried K on Saturday afternoon. Thank you E, and all!
    Received a mountain of Playmobil (12 kilos!) which will provide for Y’s birthday, and family members can subscribe to parts of it as their gift. The mountain is big enough to also cover for Sinterklaas in December I suspect.
    Worked on a provincial data publishing platform
    Spent quite a bit of time in my head on the fireworks disaster which was 20 years ago this Wednesday. That had been building in the week and a half before, after reading the now published diary from then by my friend Danny, in which I feature quite a bit.
    Went to the dentist for a next step in getting my implant tooth. Next stage in August, so for now it is done.
    Had some meetings with a provincial digital transformation team
    Had a meeting with Maltese civil servants on meteo data
    Discussed a EU project proposal with a client
    Wrote background material and sent it out for two online workshops I’m doing next week
    Participated in Oliver’s unconference, which was a good end to the work week and start of the weekend
    Got about half way in building a robot cat from the Lego kit I got, putting it together with Y

  8. A summary overview of changes I made to this site, to make it more fully a indieweb hub / my core online presence. The set-up of my WordPress installation also has been described.
    Theme related tweaks

    Created child theme of Sempress, to be able to change appearance and functions
    Renamed comments to reactions (as they contain likes, reposts, mentions etc.)
    in the entry-footer template and the comments template
    Removed h-card microformats, and put in a generic link to my about page for the author in the Sempress function sempress_posted_on. Without a link to the author mentions show up as anonymous elsewhere.
    Removed the sharing buttons I used (although they were GDPR compliant using the Sharriff plugin, but they got in the way a lot I felt.
    Added a few menu options for various aspects of my postings (books, check-ins, languages)
    Introduced several categories to deal with different content streams: Dutch, German for non-English postings, Day to Day for things not on the home page, Plazes for check-ins, Books for ehh books, RSS-Only for unlisted postings, and Micromessage for tweets I send from the blog. This allows me to vary how I display these different types of things (or not)
    Displaying last edited and created dates to (wiki)pages
    Added a widget with projects I support
    Added to the single post template a section that mentions and links the number of Hypothes.is annotations for that post, where they exist.

    Functionality related tweaks

    Started creating pages as a wiki-like knowledgebase, using page categories to create the wiki structure
    To show excerpts from webmentions I changed the template for a webmention in the Semantinc backlinks plugin, class-linkbacks-handler.php
    Added a plugin to display blogposts on the same date in previous years.
    Added plugin Widget Context to remove recent posts and comments from individual posting’s pages, as they cause trouble with parsing them for webmentions.
    Using categories as differentiator I added language mark-up to individual postings, category archives. Also added automatic translation links to non-English postings in the RSS feed (not on the site). On the front page non-English postings have language mark-up around the posting.
    Added a blogroll that is an OPML file with a stylesheet, so it can be equally read by humans and machines.
    Added an extra RSS feed for comments that excludes webmentions and ping/trackbacks
    Added a /feeds page
    Added a Now page
    Added a Hello page
    Added a way to share book lists / feeds.
    Stopped embedding slide decks, and stopped embedding new Flickr photos (as well as removed older embeds, currently 23 postings between January 2013 and July 2018 still have them, and 22 postings from June 2006 to July 2009)
    Removed all affilliate links to Amazon books as it entails tracking
    Added an Index (using a plugin)
    Added my own basic check-in and Dopplr style posts

    Other tweaks

    Set up 2 additional WordPress instances for testing purposes (Proto and Meso)

Comments are closed.

Mentions