我正在尝试为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票,但不会在单击时更新元数据。我现在不知道我做错了什么。