Post meta not updating

时间:2012-12-18 作者:vaibhav

我正在尝试为WordPress制作一个投票系统。

这是我的密码functions.php:

  add_action("wp_ajax_add_votes_options", "add_votes_options");
  add_action("wp_ajax_nopriv_add_votes_options", "add_votes_options");
   function add_votes_options() {
if (!wp_verify_nonce($_POST[\'nonce\'], \'voting_nonce\'))
    return;

$postid = $_POST[\'postid\'];
$ip = $_POST[\'ip\'];

$voter_ips = get_post_meta($postid, "voter_ips", true);
if(!empty($voter_ips) && in_array($ip, $voter_ips)) {
    echo "null";
    die(0);
} else {
    $voter_ips[] = $ip;
    update_post_meta($postid, "voter_ips", $voter_ips);
}   

$current_votes = get_post_meta($postid, "votes", true);
$new_votes = intval($current_votes) + 1;
update_post_meta($postid, "votes", $new_votes);
$return = $new_votes>1 ? $new_votes." votes" : $new_votes." vote";
echo $return;
die(0);
    }
这是我放置投票按钮和计票按钮的方式:

    <?php
// This will display "0 votes" and increase as votes are added
$votes = get_post_meta($post->ID, "votes", true);
$votes = !empty($votes) ? $votes : "0";
if($votes == 1) $plural = ""; else $plural = "s";
echo \'<div id="votecounter">\'.$votes.\' vote\'.$plural.\'</div>\';
?>

<?php
// This will display the vote button and disable it if a cookie has already
// been set. We also add the security nonce here. 
$hasvoted = $_COOKIE[\'better_votes\'];
$hasvoted = explode(",", $hasvoted);
if(in_array($post->ID, $hasvoted)) {
$vtext = "VOTED";
$class = \' class="disabled"\';
} else {
$vtext = "VOTE";
$class = "";
}
?>
<a href="javascript:void(0)" id="vote"<?php echo $class; ?>><?php echo   $vtext; ? ></a>
<?php if(function_exists(\'wp_nonce_field\')) wp_nonce_field (\'voting_nonce\', \'voting_nonce\'); ?>
问题是它没有更新元数据。它显示1票,但不会在单击时更新元数据。我现在不知道我做错了什么。

1 个回复
SO网友:Steve Claridge

服务器端代码看起来不错(乍一看),但您没有从JavaScript调用正确的服务器端代码。

看看这个:http://wp.smashingmagazine.com/2011/10/18/how-to-use-ajax-in-wordpress/. 您需要对admin-ajax.php?action=<your action>

结束

相关推荐

Functions.php Problem

当我在函数顶部添加第3-19行时。php,然后尝试更新图像。phpI get the error \"Warning: Cannot modify header information - headers already sent\" when I try to update my image.php.第3-19行的代码为“我的图像库”创建自定义的下一个/上一个链接,并将最后一个链接重定向到“更多库”页面。我的代码可以在这里看到:http://pastebin.com/Yen04t2z