0

I am trying to update post meta for a voting plugin. Specifically it calls update_post_meta() on an ajax request. Here is the code:

function add_settings_menu() {
     add_options_page('Contested Settings', 'Contested Settings', 1, 'manage_options', 'contested_settings');
}

function contested_settings() {
    include("contested_admin_settings.php");
}

function get_posts_votes($post) {
    $votes = get_post_meta($post, 'votes', true );
    return $votes;
}

function add_contested_voting($category, $post) {
    if (in_category($category, $post)) {
            wp_enqueue_script('jquery');
            $post = get_post($post);
            $post_id = (int)$post->ID;
            add_post_meta($post_id, 'votes', '0', true);
            /*if($votes == '') { 
                update_post_meta($post, 'votes', '0'); 
                $votes = get_post_meta( $post, 'votes', true ); 
            }*/
        ?>
            <form id='voting-form'>
                <strong>Vote now!</strong><br />
                <label id='votes'><?php echo get_posts_votes($post); ?></label>
                <input type='button' id='vote_up' value='+1' />
            </form>
            <script>
                $(function() {
                    ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
                    $("#vote_up").click(function() {
                        var data = {
                            action: 'contested',
                            votes: '<?php echo $votes; ?>',
                            post: '<?php echo $post_id; ?>'
                        };
                        $.post(ajaxurl, data, function(data) { 
                            $("#voting-form").html(data);
                        });
                    });
                });
            </script>
        <?
    }
}

function ajax_posted() {
    $votes_plus = $_POST['votes']+1;
    $id = $_POST['post'];
    if (update_post_meta($id, 'votes', $votes_plus)) {
        $votes_ret = get_post_meta($id, 'votes', true);
        echo "Thanks for voting! ".$votes_ret." vote.";
    } else {
        echo "Voting failed!";
    }
    die();
}

add_action('admin_menu', 'add_settings_menu');
add_action('wp_ajax_contested', 'ajax_posted');

I'm not really sure what is wrong with this. Any thoughts?

3
  • You're entirely too trusting of your $_POST variable. Please sanitize your data. (I know this doesn't answer your question, but building good habits now will save you grief later.)
    – Pat J
    Jul 15, 2013 at 18:53
  • Is your troublesome update_post_meta() the one in the if( $votes == '' ) statement? If so, I don't see where $votes is getting initialized, so I think you're comparing a null to an empty string.
    – Pat J
    Jul 15, 2013 at 18:56
  • I know I need to sanitize the $_POST variable, just trying to figure this out first. :) And no the troublesome one is in the ajax_posted() variable. It is returning true and echoing the value just like it should. I cant determine if the variable is getting over written somewhere else or if the update post that says its workign really isn't.
    – wuz
    Jul 16, 2013 at 13:04

1 Answer 1

3

I figured it out. Something was wrong with using '$id' in update_post_meta.

I changed this:

function ajax_posted() {
    $votes_plus = $_POST['votes']+1;
    $id = $_POST['post'];
    if (update_post_meta($id, 'votes', $votes_plus)) {
        $votes_ret = get_post_meta($id, 'votes', true);
        echo "Thanks for voting! ".$votes_ret." vote.";
    } else {
        echo "Voting failed!";
    }
    die();
}

to this:

function ajax_posted() {
    $id = intval($_POST['post']);
    $post = get_post($id);
    $votes = get_posts_votes($post);
    $votes_plus = $votes+1;
    if (update_post_meta($post, 'votes', $votes_plus)) {
        $votes_ret = get_post_meta($id, 'votes', true);
        echo "Thanks for voting! ".$votes_ret." vote.";
    } else {
        echo "Voting failed!";
    }
    die();
}
1
  • 1
    I AM voting up for finding and solving ur problem and updated in the stackexchange.
    – Balas
    Jul 4, 2014 at 6:21

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.