Extend WP-PostRatings plugin to limit votes by username and post category

This is an extension of the Extend WP-PostRatings plugin to limit votes by username tutorial

Tutorial

In this tutorial I will show you how to limit votes per username in WP-PostRatings plugin for WordPress. We also limit votes for each category you have in a time range that we select.

Let’s assume we declared 10 different categories for posts. This extension will block users to vote more than once on posts belonging to a particular category. So in a list of posts the user if he has voted in one category more than once will not be able to vote and in another category that has not reached the x votes he will be able to vote normally. All of this according to a time limit we will set.

Compared to the previous tutorial about WP-PostRatings we need the term_taxonomy_id which is located in _term_relationships table which is also stored in your WordPress database.

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 );
	
	$categories = get_the_category();
	$category_id = $categories[0]->cat_ID;
	//24LtNs6_term_relationships
	$result = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb- 
        >ratings},24LtNs6_term_relationships WHERE rating_userid = %d AND
        from_unixtime(`rating_timestamp`) > date_sub(now(), interval 7 day) AND 
        term_taxonomy_id = %d AND rating_postid = object_id;", 		           
        $rate_userid, $category_id) );
	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.

What you should note is the name of the database table, you should change 24LtNs6_term_relationships with yourdbprefix_term_relationships.

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 );
	
	$categories = get_the_category();
	$category_id = $categories[0]->cat_ID;
	//24LtNs6_term_relationships
	$result = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb- 
        >ratings},24LtNs6_term_relationships WHERE rating_userid = %d AND
        from_unixtime(`rating_timestamp`) > date_sub(now(), interval 7 day) AND 
        term_taxonomy_id = %d AND rating_postid = object_id;", 		           
        $rate_userid, $category_id) );
	return $result;
}

Conclusion

So thats the second part of this extension, if you didn’t read the simplest one here it is. In this tutorial we learned how to expand WP-PostRatings and limit votes per username in a time range and also for each post category. If you got any questions feel free to comment down below.

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on skype
Share on whatsapp

Leave a Comment

Your email address will not be published. Required fields are marked *