fbpx

Extend WP-PostRatings plugin to limit votes by username

I was working on a project that had a photography and video contest. So I searched and found many plugins for wordpress that allow me to vote posts that users have uploaded and even vote pages. I tried many plugins and i conclude that WP-PostRatings plugin was the simplest one and the best one without any extra features in a pro version.

I was wondering on how to limit votes for a post or page for a specific user in Wordpress so i google searched it. How to limit votes by username? wp post ratings limit votes by username? or wp post ratings vote once per day.

I found some posts where developers were designing their own rating method. Ι thought since there is this great plugin why can’t we just expand it. So i did.

Tutorial

In this tutorial I will show you how to limit votes per username in WP-PostRatings plugin for Wordpress. We do not simply reduce it to 2 or 3 votes per user but at the same time we reduce it to a specific time. After this time the user can vote again, let’s say twice.

If you want to expand it even further check the Extend WP-PostRatings plugin to limit votes by username and post category.

Understanding WP-PostRatings

Wp post ratings plugin is a well written plugin with action and filters hooks so you can expand or filter data. We are not going to use this functionality but instead we will take a look in plugin’s database.

After installing WP-PostRatings another database table will appear in your Wordpress database named yourdbprefix_ratings. Inside _rating table you will find all the information you need about the votes. In this tutorial we care about rating_userid column and rating_timestamp column. The first one is the user_id that votes the specific post and the second one the time that did this action.

Solution

We have to limit the votes according to a variable so we declare a global variable for it.

$GLOBALS['myvalue'] = 5;

That variable will represent the times that a specific user can vote in a limited time.

Note: I write my own scripts in snippets using the snippet plugin for Wordpress to keep the code clean and be sure to not break anything but you can write your code inside php file.

add_shortcode( 'myshortcode', 'showPermissionResult' );
function showPermissionResult(){
	$showMessage = do_shortcode('Vote - Like: [ratings]');
	//call function getVoteCount()
	$result = "getVoteCount";
	$result = call_user_func($result);
	
	if($result >= $GLOBALS['myvalue']){
	  $showMessage = "Υou can't vote anymore this week";
  }
	return $showMessage;
}

WP-PostRating uses a shortcode so we can put the voting extension when ever we want.

The above function runs that shortcode with a string in front of that encouraging users to vote here. We check if the total votes that this user made the last 7 days are above or equal to 5(Our limit). If that is not true we run the shortcode as it is. Else we show a message instead of a shortcode and informing user that can’t vote anymore.

To make that action happen and rotate from a shortcode to a normal string we use our custom shortcode

add_shortcode( 'myshortcode', 'showPermissionResult' );

so we can display the result of the showPermissionResult() function.

function getVoteCount(){
   global $wpdb,$user_ID;
   $rate_userid = apply_filters( 'wp_postratings_process_ratings_userid', $user_ID );
   $result = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->ratings} WHERE 
   rating_userid = %d AND
   from_unixtime(`rating_timestamp`) > date_sub(now(), interval 7 day);", $rate_userid) ); 
   return $result;
}

The above function help us collect the vote count for the past 7 days. You can change the interval 7 days with any number you want.

The Whole Code

$GLOBALS['myvalue'] = 5;

add_shortcode( 'myshortcode', 'showPermissionResult' );
function showPermissionResult(){
	$showMessage = do_shortcode('Vote - Like: [ratings]');
	//call function getVoteCount()
	$result = "getVoteCount";
	$result = call_user_func($result);
	
	if($result >= $GLOBALS['myvalue']){
	  $showMessage = "Υou can't vote anymore this week";
  }
	return $showMessage;
}

function getVoteCount(){
   global $wpdb,$user_ID;
   $rate_userid = apply_filters( 'wp_postratings_process_ratings_userid', $user_ID );
   $result = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->ratings} WHERE 
   rating_userid = %d AND
   from_unixtime(`rating_timestamp`) > date_sub(now(), interval 7 day);", $rate_userid) ); 
   return $result;
}

Conclusion

So there you have it. In this tutorial we learned how to expand WP-PostRatings and limit votes per username in a time ranged. If you got multiple categories for your posts and you want to limit votes per user for a specific category in a time range check this tutorial.

Μετάβαση στο περιεχόμενο