0

I've been researching this for a few days now and found many voting plugins - unfortunately none of them does 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'))
    return;

$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";
    die(0);
} 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;
die(0);
}

2 Answers 2

2

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 );
4
  • The only thing to add here is accounting for a race condition. Commented Jul 19, 2012 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
    Commented Jul 20, 2012 at 14:17
  • @ThadBloom Use your browser’s network logger to inspect the AJAX request.
    – fuxia
    Commented Jul 20, 2012 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
    Commented Jul 20, 2012 at 17:36
0

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 = "";
}
1
  • 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.
    – fuxia
    Commented Jul 21, 2012 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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