WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been researching this for a few days now and found many voting plugins - unfortunately none of them do exactly what I need, so I am trying to hack some functions/scripts that I've found by searching online.

I'm working with this script, now: http://bavotasan.com/2011/a-better-voting-system-for-wordpress/

I'm trying to figure out a way to only allow the IP to vote once on the website. Essentially, if they vote on one post - they cannot vote on another.

This post is similar to this thread, Wordpress Vote Plugin - Vote Once and Track User

add_action("wp_ajax_add_votes_options", "add_votes_options");
add_action("wp_ajax_nopriv_add_votes_options", "add_votes_options");
function add_votes_options() {
if (!wp_verify_nonce($_POST['nonce'], 'voting_nonce'))

$postid = $_POST['postid'];
$ip = $_POST['ip'];

//$voter_ips = get_post_meta($postid, "voter_ips", true);

// use an option instead

$voter_ips = get_option( "wpse_59080_voter_ips", true );

if(!empty($voter_ips) && in_array($ip, $voter_ips)) {
    echo "null";
} else {
    $voter_ips[] = $ip;
    // update_post_meta($postid, "voter_ips", $voter_ips);
    update_option( "wpse_59080_voter_ips", $voter_ips );


//$current_votes = get_post_meta($postid, "votes", true);
$current_votes = get_option( "wpse_59080_voter_ips", true );

$new_votes = intval($current_votes) + 1;

// update_post_meta($postid, "votes", $new_votes);

// use an option instead

update_option( "wpse_59080_voter_ips", $voter_ips );

$return = $new_votes>1 ? $new_votes." votes" : $new_votes." vote";
echo $return;
share|improve this question

The only difference you need is the place where the IP address will be stored. Instead of a post meta use an option.

So, where the script says …

$voter_ips = get_post_meta($postid, "voter_ips", true);

… use …

$voter_ips = get_option( "wpse_59080_voter_ips", true );

… and where it updates a post meta you should update the option:

update_option( "wpse_59080_voter_ips", $voter_ips );
share|improve this answer
The only thing to add here is accounting for a race condition. – Brian Fegter Jul 19 '12 at 21:46
Thanks toscho, I was about to go about this in a very different way. I've updated my script, however now it doesn't seem to be recording any votes in the admin. I've updated my original post to show what I changed – Thad Bloom Jul 20 '12 at 14:17
@ThadBloom Use your browser’s network logger to inspect the AJAX request. – toscho Jul 20 '12 at 16:41
@toscho It seems to be firing when clicked, it does take quite a bit of time to execute, 1.12s... I'm going to scour the files to see if I can find anything else that might need changed. I'm a bit of a novice when it comes to PHP/JS, so hopefully I can figure this one out. Thanks again for pointing me in the right direction. – Thad Bloom Jul 20 '12 at 17:36

I ended up doing a direct SQL query on the entire post_meta table, and then displaying the message "VOTED" if the IP address has already been recorded in the DB:

$voter_ips = $_SERVER['REMOTE_ADDR']; 
$result = mysql_query("SELECT * FROM  wp2_postmeta WHERE meta_value LIKE '%$voter_ips%' ");
$num_rows = mysql_num_rows($result);

if ($num_rows > 0){
          $vtext = "VOTED";
          $class = ' class="disabled"';
} else {
$vtext = "VOTE";
$class = "";
share|improve this answer
This is bad. $_SERVER cannot be trusted, it may contain malicious code. You should prepare everything you send to the DB. And using mysql_query() is very … 1999. – toscho Jul 21 '12 at 1:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.