Creating a Plugin to Add Votes to Your WordPress Comments Using AJAX

WordPress has a good posting system which is extendable so that WordPress can be used to build a lot of different sites rather than just a blog. WordPress also has a very extendable commenting system. The commenting system of WordPress can also be extended in different ways to be used in your WordPress sites. The commenting system can be used as a review forum if the WordPress site is to be made into a product catalog system. The commenting system can be used as answers to questions if the WordPress site is to be used for a Question and Answer site. So the commenting system in WordPress can be moulded or extended to provide functionality th at can enhance the complete site built on WordPress. In this article we are going to write a plugin to add a voting system on the comments which could be useful if the commenting system is used in a different way like in the examples stated above and many others that you can think up.


Step 1 Creating the Plugin

Let's now start by creating the plugin. In our wp-content folder we will create a folder for our plugin called commentsvote. Inside the folder commentsvote create a file commentsvote.php which will be the main file of our plugin. Then we will add the plugin header as follows which will help WordPress to identify our plugin and show it in the plugin list in WordPress' admin. The header is as follows.

Once we add the header we will be able to see our plugin in the plugin list as follows. We can now activate the plugin.

Now let's setup some variables and scripts which we will use in our plugin. We create constants to define the plugin path and the plugin URL which can be used in different places in the plugin as follows.

Also as we are going to have the functionality to add votes via AJAX we will need a JavaScript file. To add the JavaScript file we create a js folder in our plugin folder and create a commentsvote.js file in it. Once we have created the necessary files our folder structure for the plugin will look as follows.

Then we enqueue the scripts so that the JS files are loaded by the WordPress engine as follows.


Step 2 Understanding Some WordPress Function for Comments

WordPress provides support for comments meta in which we can store and retrieve some meta data related to the comment. We are going to store the votes on the comments as comment meta using these supporting functions. Hence here we are going to see what these functions are which WordPress provides.

The first function which we are going to see is get_comment_meta(). This function is similar to get_post_meta(). This function takes the first argument as the comment ID, the second argument as the name of the meta key, the third argument which if set to true returns the value as a string, if set to false or left blank will return an array of all the values for that key.

For more information you can visit the get_comment_meta() page in the WordPress Codex.

The other function which we are going to use is update_comment_meta. This function takes four arguments which are the comment ID, the meta key, the meta value and the previous value.

For more information you can visit the update_comment_meta() page in the WordPress Codex.


Step 3 Displaying the Current Votes Next to the Comment

Now let's add the number of votes and a vote link next to each comment so that the users can vote on the comments. For this first we write a function which will create the link and return it as a string.

In this function we first get the current comment ID using the WordPress function get_comment_ID(). Then we get the number of votes on comment the using the function get_comment_meta().

Then we create a link in which we call the JavaScript function commentsvote_add() which we will implement in the below steps.

Then let's add this link to every comment using the filter comment_text. We hook into the filter comment_text and add the link as follows.

Now if you visit the individual post page you will see the vote's link as follows.


Step 4 Creating the AJAX Handler for Adding Votes on Comments

Now once we have created the code to show the link let's create the AJAX handlers in WordPress to handle the request. The AJAX request handler is as follows.

In the AJAX handler we first verify the nonce. Then we get the comment ID and get the number of current votes on the comment using the function get_comment_meta(). Once that is done we increment the current count by one and then update the meta using the function update_comment_meta().

Then we return the updated value back from the AJAX handler.


Step 5 Voting on Comments via AJAX

Now once our link and the AJAX handlers are done we just need to write the JavaScript function to make the AJAX call. The function is as follows.

The function basically gets the comment ID and the makes an AJAX call to the AJAX handler. Once the AJAX handler returns the value the JavaScript function retrieves the div of the comment and updates its content with the new number of votes. Now one can click on the vote to add one vote to the comment.


Step 6 Allowing Only Logged in Users to Vote

Now let's enhance the plugin to give an option to the admin to allow only logged in users to vote on comments. To do this we first create a settings page for our plugin. Following is the code to create a settings page.

Now depending on the setting we will check whether the user is logged in or not before showing the vote link or make the link point to the login page. So the adapted functions commentsvote_showlink() and commentsvote_ajaxhandler() will look as follows.

So now in case if the admin makes it compulsory that logged in users can only vote and if the logged in user is not logged in he will be pointed to the login page as follows.


Step 7 Showing Most Vote Comments on the Post

Now let's add the functionality to show below each post its top voted posts. To do this we will first have to fetch the comments for the post in descending order of its votes. There is no direct way to sort comments on the basis of their meta hence we will write a custom query for the same. We will write the following function to do it.

This function takes posts as input and then runs a custom query on the wp_comments and wp_commentmeta to fetch the list of top voted comments for the post.

Then we add a function to the filter the_content as follows to show the comment excerpt and the votes below the post.

Now if we see the post it will look as follows.


Conclusion

In case WordPress is used for different kind of sites its basic system and functionalities might not be enough for the site. But as the WordPress system is very extendable we can write plugins like the above to provide extra functionality to suit our needs. So have fun while extending your WordPress site!

Tags:

Comments

Related Articles