Create an Author Comments Feed in WordPress

Posted in Plugins, WordPress • Posted on 4 Comments

In recent years blogs have become so popular that and they’re one of the most common ways people get their daily dose of information. It’s not possible for a single person to provide quality content everyday. As a result, major websites are accepting  guest authors to provide quality content on a regular basis.

Authors play a vital role in these multi-author blogs since they are responsible for providing up-to-date content for their readers. Once a post is published, the author has to respond to comments, fix the issues and update the content according to the latest trends. As an Author, it is important to respond to user comments quickly. When a comment is made, only the administrator gets the notification email to approve the comments. But there is no default functionality to notify the author.

Authors have to view comments of each post regularly and I can tell you that is very hard if you have written a large number of posts. In this tutorial I am going to ease this task by creating an author comments feed for the comments of all posts written by a single author. Then you will be able to view all the comments in one place. So Lets get started.

Plugin Download

Creating The Author Comments Plugin

Although we can create our functionality in the functions.php file of the theme, it’s always recommended to create an independent plugin for custom functionality. So let’s create a folder called Author-Comments-Feed inside the wp-content/plugins folder and insert the following code to index.php file. It is used to provide information about the plugin.

/*
  Plugin Name: Author Comments Feed
  Plugin URI: http://innovativephp.com/
  Description: Generate RSS feed for comments on all the posts of a specific author.
  Version: 1.0
  Author: Rakhitha Nimesh
  Author URI: 
  License: GPLv2 or later
 */

Now you will be able to see our plugin in the plugin list on the admin dashboard. So let’s move onto adding our RSS feed.

Adding Custom RSS Feed

WordPress provides a default set of RSS feeds for posts, comments, categories etc. Also we are allowed to create custom RSS feeds using the add_feed function. So let’s add our Author RSS feed.

function add_author_comments_feed() {
    global $wp_rewrite;
    add_feed('wp_author_comments', 'wp_author_comments_feed');
    add_action('generate_rewrite_rules', 'author_comments_rewrite_rules');
    $wp_rewrite->flush_rules();
}

add_action('init', 'add_author_comments_feed');
  • First we add add_author_comments_feed function to be called after WordPress has finished loading using the init action.
  • Then we add our feed using the WordPress add_feed function. We have to pass 2 parameters to this function.
  • First parameter will be the type of feed. I have named it wp_author_comments. Default feed types are RSS1, RSS2, ATOM.
  • Second parameter is the name of the function which is used to generate the RSS feed.
  • Then we have to define custom permalinks for the RSS feed. I have added author_comments_rewrite_rules function using the generate_rewrite_rules filter. I’ll explain these functions in the next section.
  • Finally we flush the rewrite rules structure using $wp_rewrite->flush_rules method to refresh the rewrite rule cache.

Lets move onto creating custom rewrite rules for Author Comments Feed.

Define Custom Rewrite Rules

If you are using the default URL structure of WordPress (using query strings), you don’t need to worry about this section. This section is essential if the site uses a custom permalink structure. So lets take a look at our author_comments_rewrite_rules function.

function author_comments_rewrite_rules($wp_rewrite) {

    $new_rules = array(
        'feed/(.+)/(.+)' => 'index.php?feed=' . $wp_rewrite->preg_index(1)
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;

}
  • $wp_rewrite object has all the information about the rewrite rules and it will be passed to the author_comments_rewrite_rules function by default.
  • First we prepare the new rules for the custom feed. In our URL we need feed type and author username. So we define it as feed/(.+)/(.+) and redirect to index.php with feed parameter.
  • Finally we add our new rules to the original rules structure.

Now we are ready with our custom rewriting rules and its time to create the Author Comments Feed.

Generating Author Comments Feed

I am going to use “Universel Feed Writer class” library for generating RSS feed. You can download it here.

Once downloaded add the FeedWriter.php file and FeedItem.php file to your plugin directory.

Then make sure to include FeedWriter.php file in the top of the index file. Also we need slight modification to the autoload function in the downloaded library. Following code shows the default autoload function in the library.

function __autoload($class_name){
    require_once $class_name . '.php';
}

The code above will try to autoload every class in WordPress as well and will generate errors. So modify the above function with the following code to only autoload the classes related to the library.

function __autoload($class_name){
     if($class_name == 'FeedItem'){
         require_once $class_name . '.php';
     }
}

Now lets take a look at our wp_author_comments_feed function which generates the RSS feed.

function wp_author_comments_feed() {

    global $wpdb;

    $username = isset($_GET['auth']) ? $_GET['auth'] : array_pop(explode("/", $_SERVER['REQUEST_URI'])) ;

    $user = get_user_by('login', $username);

    $sql = "SELECT $wpdb->posts.guid,$wpdb->posts.post_title,$wpdb->comments.comment_author,
$wpdb->comments.comment_author_email,$wpdb->comments.comment_date,
$wpdb->comments.comment_content FROM $wpdb->posts inner join $wpdb->comments
on $wpdb->posts.ID=$wpdb->comments.comment_post_ID WHERE
$wpdb->posts.post_author=".$user->data->ID." and $wpdb->posts.post_status='publish'";

    $numComments = $wpdb->get_results($sql);

    $TestFeed = new FeedWriter(RSS2);

    $TestFeed->setTitle('Author Comments Feed of '.$user->data->display_name);
    $TestFeed->setLink('http://www.1stwebdesigner.com');

    foreach ($numComments as $key => $comment) {

        //Create an empty FeedItem
        $newItem = $TestFeed->createNewItem();

        $description = "Comment Author Name : $comment->comment_author <br/>
				Comment Author Email : $comment->comment_author_email <br/>
				Comment : $comment->comment_content <br/>";

        //Add elements to the feed item
        $newItem->setTitle("New Comment on " . $comment->post_title);
        $newItem->setLink($comment->guid);
        $newItem->setDate($comment->comment_date);
        $newItem->setDescription($description);

        //Now add the feed item
        $TestFeed->addItem($newItem);
    }

    $TestFeed->genarateFeed();exit;
}
  • First we have to get the author’s username using the URL. We have to consider both default and custom permalink structures here.
  • In default one URL will look like ?feed=wp_author_comments&auth=username. so we first check if the username is available in query string using the $_GET['auth']
  • We get the username for the custom permalinks by using array_pop(explode(“/”, $_SERVER['REQUEST_URI'])) which splits the URL components.
  • Then we get all the author information by passing username to get_user_by function.
  • Next we get all the comments for the posts written by the author using the join on posts and comments tables. Now we have an array of all the comments for all the posts written by the author.
  • Next we initialize the FeedWriter and set the title and link of the RSS feed. You can customize these values according to your preference.
  • Then we loop through each comment and sets the information for the feed by using setTitle, setLink, setDate, setDescription methods. Then we add items to the RSS feed using addItem function at the end of each loop.
  • Finally we call the genarateFeed function to create the RSS feed and print it to the browser.

Now we have the Author Comments Feed ready. Every author can access the comments feed using ?feed=wp_author_comments&auth=username or /feed/wp_author_comments/username depending on the permalink structure.

It’s important to notice that this plugin will only work with WordPress default comment system which stores the comments on wp_comments table. Plugin will not work with custom commenting systems like DISQUS.

Now authors have their own comment feed and the problem of responding to latest comments is solved. Lets see how we can improve this by using IFTTT.

Getting Email Notifications Using IFTTT

Even though we have a comment feed, it takes time to log into your RSS reader and look for the new comments on your posts. So lets see how we can use IFTTT services to get an email directly to your inbox when the comment feed is updated. First create an account in IFTTT.com and log into your account.

Then click on the Create button in the dashborad and you will get the following screen.

Create Recipe

Click this and you will get a list of channels in the bottom of the page as shown below.

Choose Incoming Channel

Choose Feed and you will get the next screen as following.

Choose a Trigger

Choose New Feed Item and you will be directed to enter the Feed URL.

Complete Incoming Trigger Fields

Enter the URL of the feed and you username in the format which I mentioned in earlier section and click Create Trigger button to get the following screen.

Choose Outgoing Channel

Click that and choose a channel as we did before. Since we want to get an email, its better to select Gmail from the list. You will be directed to the following screen.

Choose Outgoing Action

Click Send an Email to get to the following screen.

Create Action

Enter your email address and customize the content according to your preference. Finally click the Create Action button.

Now every time the feed is updated you will receive an email to your gmail account.This method will save you a lot of time in responding to comments. Hope you enjoyed it and feel free to share your comments.

22 Written ArticlesWebsiteGoogle+

Rakhitha Nimesh is a software engineer and writer from Sri Lanka. He likes to develop applications and write on latest technologies. He is available for freelance writing and WordPress development. You can read his latest book on WordPress Web Application Development. He is a regular contributor to 1stWebDesigner, Tuts+ network and Sitepoint network. Make sure to contact him at www.innovativephp.com or follow him on Twitter and Google+

4 Comments
  • Natalie

    Friday, August 24th, 2012 02:32

    1

    Blog visitors may not notice an author’s comment right away, though. Highlighting author comments is an easy way to make them stand out. The standard comments template for WordPress themes already comes with a CSS class you can target, so you can give an author’s comment a different background than the others.

    0
    • Rakhitha Nimesh

      Friday, August 24th, 2012 22:25

      3

      Your idea is very good. Actually this feed is intended to use for authors only. Because users will not know the usernames of authors and they can see the comments in the site. I created this feed to notify authors that a new comment is available so that he can respond quickly

      Thanks for the comment.

      0
  • Nahan

    Friday, August 24th, 2012 11:25

    2

    It is obvious that single author cant comment all the post that he/she writes. Creating a comment feed for author helps them in connecting with their visitors. Nice post with tutorials “)

    0
  • Arafin Shaon

    Thursday, August 30th, 2012 15:24

    4

    Simply brilliant buddy! Thanks for sharing such a killer tips with us :)

    0
  • Arafin Shaon

    Thursday, August 30th, 2012 15:24

    4

    Simply brilliant buddy! Thanks for sharing such a killer tips with us :)

    0
  • Nahan

    Friday, August 24th, 2012 11:25

    2

    It is obvious that single author cant comment all the post that he/she writes. Creating a comment feed for author helps them in connecting with their visitors. Nice post with tutorials “)

    0
  • Natalie

    Friday, August 24th, 2012 02:32

    1

    Blog visitors may not notice an author’s comment right away, though. Highlighting author comments is an easy way to make them stand out. The standard comments template for WordPress themes already comes with a CSS class you can target, so you can give an author’s comment a different background than the others.

    0
    • Rakhitha Nimesh

      Friday, August 24th, 2012 22:25

      3

      Your idea is very good. Actually this feed is intended to use for authors only. Because users will not know the usernames of authors and they can see the comments in the site. I created this feed to notify authors that a new comment is available so that he can respond quickly

      Thanks for the comment.

      0

Comments are closed.

54.211.157.103 - unknown - unknown - US