Note that there are some explanatory texts on larger screens.

plurals
  1. POpost rating system function
    text
    copied!<p>There's code below for simple post rating system (tutorial from this site <a href="http://wp.tutsplus.com/tutorials/how-to-create-a-simple-post-rating-system-with-wordpress-and-jquery/" rel="nofollow">TUTORIAL</a> ) - it works fine but i need to change it a bit. Now where user votes, he is not able to vote again for same post for $timebeforevote (1440 minutes). I want to block user voting for all post not just for this one.</p> <p>I think the problem is function is looking for user ip in current post meta field with this code:</p> <pre><code>$meta_IP = get_post_meta($post_id, "voted_IP"); $voted_IP = $meta_IP[0]; </code></pre> <p>Returns <code>$voted_IP</code> and compares it with <code>$ip = $_SERVER['REMOTE_ADDR'];</code> I tried to modify it with query (where query looks for ip in meta field for all posts) so it would search for ip in all posts but it didn't work. I am trying to make it work for 2 days and nothing works.</p> <p>POST RATING SYSTEM CODE:</p> <pre><code>$timebeforerevote = 1440; add_action('wp_ajax_nopriv_post-like', 'post_like'); add_action('wp_ajax_post-like', 'post_like'); wp_enqueue_script('like_post', get_template_directory_uri().'/js/post-like.js', array('jquery'), '1.0', 1 ); wp_localize_script('like_post', 'ajax_var', array( 'url' =&gt; admin_url('admin-ajax.php'), 'nonce' =&gt; wp_create_nonce('ajax-nonce') )); function post_like() { $nonce = $_POST['nonce']; if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) ) die ( 'Busted!'); if(isset($_POST['post_like'])) { $ip = $_SERVER['REMOTE_ADDR']; $post_id = $_POST['post_id']; $meta_IP = get_post_meta($post_id, "voted_IP"); $voted_IP = $meta_IP[0]; if(!is_array($voted_IP)) $voted_IP = array(); $meta_count = get_post_meta($post_id, "votes_count", true); if(!hasAlreadyVoted($post_id)) { $voted_IP[$ip] = time(); update_post_meta($post_id, "voted_IP", $voted_IP); update_post_meta($post_id, "votes_count", ++$meta_count); echo $meta_count; } else echo "already"; } exit; } function hasAlreadyVoted($post_id) { global $timebeforerevote; $meta_IP = get_post_meta($post_id, "voted_IP"); $voted_IP = $meta_IP[0]; if(!is_array($voted_IP)) $voted_IP = array(); $ip = $_SERVER['REMOTE_ADDR']; if(in_array($ip, array_keys($voted_IP))) { $time = $voted_IP[$ip]; $now = time(); if(round(($now - $time) / 60) &gt; $timebeforerevote) return false; return true; } return false; } function getPostLikeLink($post_id) { $themename = "twentyeleven"; $vote_count = get_post_meta($post_id, "votes_count", true); $output = '&lt;p class="post-like"&gt;'; if ($vote_count == 0) $output .= '&lt;span class="count"&gt;0&lt;span class="glosow"&gt;&lt;br&gt;głosów w rankingu&lt;/span&gt;&lt;/span&gt;'; else $output .= '&lt;span class="count"&gt;'.$vote_count.'&lt;span class="glosow"&gt;&lt;br&gt;głosów w rankingu&lt;/span&gt;&lt;/span&gt;'; if(hasAlreadyVoted($post_id)) $output .= '&lt;span title="Już głosowałeś" class="qtip alreadyvoted"&gt;&lt;/span&gt;'; else $output .= '&lt;a href="#" data-post_id="'.$post_id.'"&gt; &lt;span title="'.__('Głosuj', $themename).'"class="qtip like"&gt;&lt;/span&gt;&lt;/a&gt;'; $output .= '&lt;/p&gt;'; return $output; } </code></pre> <p>Heres code for query that finds if user already voted but problem is that after $timebeforerevote IP value stays in meta field so i can't use it. I have no more ideas for now...</p> <pre><code>$ip = $_SERVER['REMOTE_ADDR']; $query = new WP_Query( array( 'ignore_sticky_posts' =&gt; true, 'meta_key' =&gt; 'voted_IP', 'meta_value' =&gt; $ip ) ); if (0 !== $query-&gt;found_posts) { //do something } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload