Leave a Reply

1 comment

  1. You will have to make a second button for down voting. Give the upvote button a class of up and the down vote button a class of down.

    jQuery

    <?php wp_enqueue_script( 'jquery' ) ?>
    <?php wp_head(); ?>
        <script type="text/javascript">
            jQuery(document).ready(function() {
                jQuery(".vote a.up").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,
                        updown: 'up'
                    }, function(data) {
                        var votebox = ".vote" + thepost + " span";
                        jQuery(votebox).text(data);
                        jQuery(some).replaceWith('<span class="voted">Voted</span>');
                    });
                });
                jQuery(".vote a.down").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,
                        updown: 'down'
                    }, function(data) {
                        var votebox = ".vote" + thepost + " span";
                        jQuery(votebox).text(data);
                        jQuery(some).replaceWith('<span class="voted">Voted</span>');
                    });
                });
            });
        </script>
    

    vote.php

    <?php
    $file = dirname(__FILE__);
    $file = substr($file, 0, stripos($file, "wp-content") );
    
    require( $file . "/wp-load.php");
    
    $currentvotes = get_post_meta($_POST['post'], 'votes', true);
    if("up" == $_POST['updown']) {
        $currentvotes ++;
    } elseif("down" == $_POST['updown']) {
        $currentvotes --;
    }
    
    $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;
    ?>
    

    Thats all you should need to edit