Temporary but working RSS 2.0 forum topic newsfeed
-
For developers only:
As the plugin author is on leave although RSS forum feeds are in the longer term TO DO list, thought I’d share my no frills but W3C validated recent topics feed that I’m temporarily using. This is all standalone code in my child theme so zero impact on the plugin code base or database, noting no timed cache was created so you can do that part on your own. I included comments in my code to help but nothing extensive.
HUGE DISCLAIMER:
You cannot simply copy/paste my code, it has dependancies for the Ultimate Member plugin and uses its own query and functions outside of Asgaros thus some functions and features are hardcoded. Experienced developers familiar with the WordPress API, RSS, PHP/MySQL should only attempt this and — as usual, backup first, test on a non-production environment and edit for your needs. Pardon any weird formatting in this support forum, but you get the idea. Thanks.
First, in functions.php, inform WP of the feed, template and add link to HTML head:
/* ------------------------------------------------- Asgaros custom forum RSS feed setup (/feed/forum) ------------------------------------------------- Edit feed in rss-forum.php */ function cdsnCustomForumRSS(){ add_feed('forum', 'cdsnCustomForumRSSFunc'); } function cdsnCustomForumRSSFunc(){ get_template_part('rss', 'forum'); } function cdsnCustomForumRSSLink(){ $url=get_site_url(); print "<link rel='alternate' href='$url/feed/forum/' title='Cancer Defiance Support Network - Forum Topics' type='application/rss+xml' />"; } add_action('init', 'cdsnCustomForumRSS'); add_action('wp_head', 'cdsnCustomForumRSSLink'); /*
Then I created a PHP script in my child theme base directory template based on the template naming convention noted above, rss-forum.php, to handle the entire feed generation:
<?php /* Template Name: Custom RSS Template - Feedname (rss-forum.php) - Asgaros recent topics feed (RSS 2.0) */ // Configuration options: $excludeForumIDs="36"; // Comma separated parent forum ID's to exclude fron wp_forum_forums table query $postCount = 25; // The number of posts to show in the feed $truncateSize=350; // How many characters to allow for description (truncated at nearest word) // Function: Sanitize query data to ensure properly escaped and newlines converted to HTML line breaks function cdsnSanitize($str) { return nl2br(stripslashes($str),false); } // Function: Truncate a string to configured size/nearest word function cdsnTruncate($text, $length) { $length = abs((int)$length); if(strlen($text) > $length) { $text = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1...', $text); } return($text); } // BEGIN FEED OUPUT header('Content-Type: '.feed_content_type('rss-http').'; charset=UTF-8', true); print '<?xml version="1.0" encoding="UTF-8"?>'; ?> <rss version="2.0" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:media="https://search.yahoo.com/mrss/" <?php do_action('rss2_ns'); ?>> <channel> <title><?php bloginfo_rss('name'); ?> - Recent Forum Topics</title> <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" /> <link><?php bloginfo_rss('url') ?></link> <description><?php bloginfo_rss('description'); ?>...</description> <lastBuildDate><?php print mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate> <language>en-us</language> <sy:updatePeriod><?php print apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod> <sy:updateFrequency><?php print apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency> <?php // Query for topic data $query=" SELECT p1.text, p1.id, p1.date, p1.parent_id, p1.author_id, t.name, u.display_name, um.meta_value as avatar, (SELECT COUNT(id) FROM wp_forum_posts WHERE parent_id=p1.parent_id) AS post_counter FROM wp_forum_posts AS p1 LEFT JOIN wp_forum_posts AS p2 ON (p1.parent_id = p2.parent_id AND p1.id > p2.id) LEFT JOIN wp_forum_topics AS t ON (t.id = p1.parent_id) LEFT JOIN wp_forum_forums AS f ON (f.id = t.parent_id) LEFT JOIN wp_users AS u ON (u.id = p1.author_id) LEFT JOIN wp_usermeta AS um ON (um.user_id = p1.author_id and meta_key = 'profile_photo') WHERE p2.id IS NULL AND f.parent_id NOT IN ($excludeForumIDs) ORDER BY t.id DESC LIMIT $postCount;"; // Execute our query $posts = $wpdb->get_results($query); ?> <?php foreach ($posts as $post) { // Prepare our topic link as this is not stored in the database $url=get_site_url(); $link="{$url}/cdsn-forum/?view=thread&id={$post->parent_id}=&part=1#postid-{$post->id}"; // Prepare our avatar handled by Ultimate Member plugin $avatar_info=new SplFileInfo($post->avatar); $avatar_ext=$avatar_info->getExtension(); $avatar="{$url}/wp-content/uploads/ultimatemember/{$post->author_id}/profile_photo-190.{$avatar_ext}"; unset ($avatar_info); // Ensure we have a default author name if missing $post->display_name=(!empty($post->display_name)) ? $post->display_name : 'CDSN Member'; ?> <item> <title><?php print strip_tags(cdsnSanitize($post->name)); ?></title> <link><![CDATA[<?php print cdsnSanitize($link); ?>]]></link> <pubDate><?php print mysql2date('D, d M Y H:i:s +0000', cdsnSanitize($post->date)); ?></pubDate> <dc:creator><?php print cdsnSanitize($post->display_name); ?></dc:creator> <guid isPermaLink="false"><?php print $url; ?></guid> <description><![CDATA[<?php print cdsnTruncate(cdsnSanitize($post->text),$truncateSize); ?>]]></description> <media:thumbnail url='<?php print $avatar; ?>' height='190' width='190' /> </item> <?php } ?> </channel> </rss>
I will offer only limited support and remind you this is temporary/standalone and not affiliated with @asgaros who I know will do a more comprehensive job using his code base to simplify.
Not to mention a fully realized feed system should include forum posts, not just topics as I’ve done — I based my SQL query on the forum topic widget code but customized in my template script.
Enjoy, to anyone who tries now prior to plugin updates.
Cheers. ??
- The topic ‘Temporary but working RSS 2.0 forum topic newsfeed’ is closed to new replies.