1stwebdesigner |
| Create an Author Comments Feed in WordPress Posted: 23 Aug 2012 06:00 AM PDT 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 DownloadCreating The Author Comments PluginAlthough 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: http://innovativephp.com/about/ 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 FeedWordPress 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');
Lets move onto creating custom rewrite rules for Author Comments Feed. Define Custom Rewrite RulesIf 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; }
Now we are ready with our custom rewriting rules and its time to create the Author Comments Feed. Generating Author Comments FeedI 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; }
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 IFTTTEven 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.
Click this and you will get a list of channels in the bottom of the page as shown below.
Choose Feed and you will get the next screen as following.
Choose New Feed Item and you will be directed to enter the Feed URL.
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.
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.
Click Send an Email to get to the following screen.
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. |
| You are subscribed to email updates from 1stwebdesigner To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
| Google Inc., 20 West Kinzie, Chicago IL USA 60610 | |







Niciun comentariu:
Trimiteți un comentariu