Okay I posted a question yesterday, that can be found here.

I've found some code on the wonderful internet that I think is really the ticket for me, but I don't know how to develop into what I really need. Here is the breakdown -

I need three separate vote links. I need to change the word "Vote" on each to something else. IE: Good/Meh/Bad. (I think I can figure this one out)

I'm hoping there is a simple and easy way to slap another two vote links into this code, because the code itself is really simple and easy. Just beg pardon my lack of PHP/Jquery knowledge.

Header code:

<?php wp_enqueue_script( 'jquery' ) ?>
<?php wp_head(); ?>
<script type="text/javascript">
jQuery(".vote a").click(
function() {
var some = jQuery(this);
var thepost = jQuery(this).attr("post");
var theuser = jQuery(this).attr("user");
jQuery.post("<?php bloginfo('template_url'); ?>/vote.php", {user: theuser, post: thepost}, 
function(data) {
var votebox = ".vote"+thepost+" span";
jQuery(some).replaceWith('<span class="voted">Voted</span>');

vote.php file:

$file = dirname(__FILE__);
$file = substr($file, 0, stripos($file, "wp-content") );

require( $file . "/wp-load.php");

$currentvotes = get_post_meta($_POST['post'], 'votes', true);
$currentvotes = $currentvotes + 1;

$voters = get_post_meta($_POST['post'], 'thevoters', true);
if(!$voters) $voters = $_POST['user']; else $voters = $voters.",".$_POST['user'];

update_post_meta($_POST['post'], 'votes', $currentvotes);
update_post_meta($_POST['post'], 'thevoters', $voters);

echo $currentvotes;

Functions.php reference:

function voting($id) {
global $user_ID;
$currentvotes = get_post_meta($id, 'votes', true);
$voters = get_post_meta($id, 'thevoters', true);
$voters = explode(",", $voters);
foreach($voters as $voter) {
 if($voter == $user_ID) $alreadyVoted = true;

if(!$currentvotes) $currentvotes = 0;
echo '<div class="vote vote'.$id.'"><span>'.$currentvotes.'</span>';
if($user_ID && !$alreadyVoted) echo '<br /><a post="'.$id.'" user="'.$user_ID.'">'.__("Vote").'</a>';
if($user_ID && $alreadyVoted) echo '<br /><span class="voted">'.__("Voted").'</span>';
echo '</div>';
if(!$user_ID) echo '<div class="signup"><p><a href="'.get_bloginfo('url').'/wp-login.php?action=register">'.__('Register').'</a> '.__('to vote').'.</p></div>';

Source has a demo.

Any and all help with this would be really, really appreciated. Thanks in advance.

1 Answer 1


Here's a simple solution. Append the ratings to the end of the post content:

add_filter('the_content', 'add_ratings_to_content', 4);

// remove ratings from excerpts
add_filter('get_the_excerpt', create_function('', 'remove_filter("the_content", "add_ratings_to_content", 6); return;'), 5);

function add_ratings_to_content($content){
  global $post;
  if(is_single()) $content .= the_ratings($post->ID);
  return $content;

Displays the current rating:

function get_current_rating($post_id){
  $votes = get_option("votes_{$post_id}");

  // handle your rating format here
  $output = '';
  if(isset($_COOKIE["rated_{$post_id}"])) $output .= "Your rating: ".esc_attr($_COOKIE["rated_{$post_id}"]);
    $output .= isset($votes['bad']) ? "{$votes['bad']} people think this sucks." : null;
    $output .= isset($votes['meh']) ? "{$votes['meh']} people said meh." : null;
    $output .= isset($votes['good']) ? "{$votes['good']} people think this post was good." : null;
    $output = "no votes yet";
  return $output;

The ajax request. Use $_SERVER['REMOTE_ADDR'] to do a IP check for multiple votes because cookies can be deleted easily (and store the IP+post id somewhere, like a transient)

add_action('init', 'process_vote');
function process_vote() {
    $rating = esc_attr($_GET['vote']);
    $post_id =  esc_attr($_GET['post_id']);

    $already_voted = isset($_COOKIE["rated_{$post_id}"]) ? true : false;
    $current_rating = get_current_rating($post_id);

    if ($post_id && in_array($rating, array('bad', 'meh', 'good')) && !$already_voted):
      // update db
      $votes = get_option("votes_{$post_id}");
      $votes[$rating] = isset($votes[$rating]) ? ($votes[$rating]+1) : 1;
      update_option("votes_{$post_id}", $votes);

      setcookie("rated_{$post_id}", $rating, time() + (86400 * 30)); // 30 day cookie
      echo get_current_rating($post_id);
      echo "Already voted?";


the HTML for the ratings, simple stuff, no css:

function the_ratings($post_id = false, $disabled = false){
  $already_voted = isset($_COOKIE["rated_{$post_id}"]) ? true : false;

  ob_start(); ?>
  <?php if(!$already_voted && !$disabled): ?>
  <p>How would you rate this?</p>
  <ul class="vote-process">
    <li><a rel="<?php echo $post_id; ?>">bad</a></li>
    <li><a rel="<?php echo $post_id; ?>">meh</a></li>
    <li><a rel="<?php echo $post_id; ?>">good</a></li>
  <?php endif; ?>

  <div class="vote-status">
    <?php echo get_current_rating($post_id); ?>
 return ob_get_clean();

The JavaScript, included in the page footer.

add_action("wp_footer", "ratings_js");
function ratings_js(){ ?>
  <script type="text/javascript">
    /* <![CDATA[ */

      $(".vote-process a").click(function () {

        var id = $(this).attr('rel');
        var post = $("#post-"+id);
        var link = $(this).html();

          type: "GET",
          url: "<?php echo home_url('/'); ?>",
          data: {
            post_id: id,
            vote: link

          beforeSend: function() {
            post.find(".vote-status").html("Please wait...");
          success: function(response){

    /* ]]> */

Remove the votes_postid option when a post is deleted:

add_action('delete_post', 'remove_votes');
function remove_votes($post_id){
  return $post_id;

I'd still recommend using CDNvote, because the code is very simple and flexible, and uses it's own database table...

  • Wow! and all called in functions.php too, easy to manipulate. Thanks so much! This is exactly what I needed, and it's easy for me to read, so I can edit it in some ways without going insane. I really appreciate your help, and the time you took to do this.
    – Lynne
    Commented Jan 14, 2011 at 22:05
  • Hey, AA. I don't know if this notifies you if you get an update. I hate to e-mail you personally and I don't think this constitutes completely new post. I've been playing with CDNvote and I can see its potential, but really until I take my PHP/SQL class this semester, I'm just not able to work with the code very well. Thankfully I do have a javascript book, but I think it works a little differently inside wordpress. The code you gave me is great and I really have no complaints. I'm just wondering if there's a way for me to show the post data on exceprts. <?php get_current_rating($post_id) ?>..
    – Lynne
    Commented Jan 17, 2011 at 18:43
  • ^^^ Doesn't work properly. It shows 'no votes yet' which makes me think that I'm doing something right, however it does not tie in to the vote count properly. Is there a way to do this or does that require a datastructure?
    – Lynne
    Commented Jan 17, 2011 at 18:44
  • nevermind........ FYI - wordpress.org/extend/plugins/emo-vote/installation
    – Lynne
    Commented Jan 18, 2011 at 0:39

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.