1

I am creating post voting system which will allow visitors to rate post (from 1 to 5). It is working fine if user is logged in, or there is no any caching plugin installed. But for guest with caching plugin activated (WP super cache) it is not working.

Can anybody help me to make it work for guest users too? I guess it needs some Ajax/JavaScript?

<form method='POST' action="">
    <input type="hidden" name="rate" value=2>
    <input type=submit value=2>
</form>
<form method='POST' action="">
    <input type="hidden" name="rate" value=3>
    <input type=submit value=3>
</form>
<form method='POST' action="">
    <input type="hidden" name="rate" value=4>
    <input type=submit value=4>
</form>
<form method='POST' action="">
    <input type="hidden" name="rate" value=5>
    <input type=submit value=5>
</form>

<? php
if (isset($_POST['rate'])) {
    if ($_POST['rate'] == '1') {
        $rate = get_post_meta($post - > ID, 'up', true);
        $rate = $rate + 1;
        update_post_meta($post - > ID, 'up', $rate);

        $total = get_post_meta($post - > ID, 'total', true);
        $ratetotal = $total + 1;
        update_post_meta($post - > ID, 'total', $ratetotal);
    }
    if ($_POST['rate'] == '2') {
        $rate = get_post_meta($post - > ID, 'up', true);
        $rate = $rate + 2;
        update_post_meta($post - > ID, 'up', $rate);

        $total = get_post_meta($post - > ID, 'total', true);
        $ratetotal = $total + 1;
        update_post_meta($post - > ID, 'total', $ratetotal);
    }
    if ($_POST['rate'] == '3') {
        $rate = get_post_meta($post - > ID, 'up', true);
        $rate = $rate + 3;
        update_post_meta($post - > ID, 'up', $rate);

        $total = get_post_meta($post - > ID, 'total', true);
        $ratetotal = $total + 1;
        update_post_meta($post - > ID, 'total', $ratetotal);
    }
    if ($_POST['rate'] == '4') {
        $rate = get_post_meta($post - > ID, 'up', true);
        $rate = $rate + 4;
        update_post_meta($post - > ID, 'up', $rate);

        $total = get_post_meta($post - > ID, 'total', true);
        $ratetotal = $total + 1;
        update_post_meta($post - > ID, 'total', $ratetotal);
    }
    if ($_POST['rate'] == '5') {
        $rate = get_post_meta($post - > ID, 'up', true);
        $rate = $rate + 5;
        update_post_meta($post - > ID, 'up', $rate);

        $total = get_post_meta($post - > ID, 'total', true);
        $ratetotal = $total + 1;
        update_post_meta($post - > ID, 'total', $ratetotal);
    }
}

$score = get_post_meta($post - > ID, 'up', true);
$number_of_votes = get_post_meta($post - > ID, 'total', true);
$average = round($score / $number_of_votes, 1);

echo $average;;
echo ' / 5 ';
echo " - ".get_post_meta($post - > ID, 'total', true)." votes"; ?>
4
  • 2
    Please, put quotes aroud your input type and name
    – Hearner
    Aug 26, 2015 at 14:26
  • 2
    You can use a loop in your php instead of getting a very long code like that..
    – Hearner
    Aug 26, 2015 at 14:29
  • 1
    @Hearner , attribute quotes are optional in HTML5 (although still recommended by most people).
    – rnevius
    Aug 26, 2015 at 15:16
  • @Hearner , I put quotes aroud your input type and name, like mevius said attribute quotes are optional in HTML5. Still, thanks both of you. Aug 27, 2015 at 7:20

0

Your Answer

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

Browse other questions tagged or ask your own question.