in CMS

How to create a wordpress post rating plugin?

While I was writing Right Rater, I wanted to have a rating system for the site that could allow people to rate the posts on the website. Though there are many plugins out there, the one that I wanted was not available easily. Either they lack functionality in some way or the other or they were expensive. So I decided to write a plugin myself so as to enable a rating system on my site. In this tutorial, I will give you step by step procedure to write a wordpress plugin for having rating system on your website.

Before we begin

This tutorial discusses some advanced aspects of wordpress plugin development. If you are unfamiliar with wordpress plugin development, I recommend you read the plugin development basics on wordpress codex. Features We target our plugin to have following features.

Multiple ratings for a given post
Applying ratings according to category of post
Optional automatic start and end date for ratings
Ajax implemented rating system
Customized rating system

Let’s begin

During the development of this plugin, we will be dividing all the code into a number of files for easy maintenance. The directory structure that we’ll be having will finally look with following files populated.

/wp-constants.php
/wp-db.php
/wp-multiratings.php
/multiratings-types.php
/multiratings-types-posts.php
/multiratings-options.php
/post_rating.php
/wp-multiratings.css
/wp-multiratings.js

Step 1

Create a file wp-constants.php to store the constants that our plugin will be using.
Write the following code in your wp-constants.php file.

<?php
 define("WP_MULTIRATING_POSTEDRATINGS_TABLE","mr_postedratings");
 define("WP_MULTIRATING_RATINGS_TABLE","mr_rating_type");
 define("PLUGIN_DIR",  '/wp-content/plugins/wp-multiratings');
?>

Here we declare three constants.

WP_MULTIRATING_POSTEDRATINGS_TABLE stores the name of the table that will store the ratings against a post.
WP_MULTIRATING_RATINGS_TABLE stores the name of table that will store various types of ratingsĀ  e.g In case of a song download website, the ratings can be Music, Lyrics, Singing etc.
PLUGIN_DIR stores the name of directory where our plugin files will reside.

Write a Comment

Comment