我正在制作一个投票系统
问题是post id返回为空
我确信post id返回为空
Bcoz当我在那里输入一个真正的post id值时,它似乎起作用了
例如
update_post_meta(123, "votes" , $newvotes)
工作正常
但是
update_post_meta($postid, "votes" , $newvotes)
工作不正常
这是代码
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 = $current_votes + 1;
update_post_meta($postid, "votes", $new_votes);
$return = $new_votes>1 ? $new_votes." votes" : $new_votes." vote";
echo $return;
die(0);
}
我也试过这个
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($_REQUEST["post_id"], "voter_ips", true);
if(!empty($voter_ips) && in_array($ip, $voter_ips)) {
echo "null";
die(0);
} else {
$voter_ips[] = $ip;
update_post_meta($_REQUEST["post_id"], "voter_ips", $voter_ips);
}
$current_votes = get_post_meta($_REQUEST["post_id"], "votes", true);
$new_votes = intval($current_votes) + 1;
update_post_meta($_REQUEST["post_id"], "votes", $new_votes);
$return = $new_votes>1 ? $new_votes." votes" : $new_votes." vote";
echo $return;
die(0);
}
但这些代码都不起作用
编辑#1在jQuery调用我的操作
$("#vote").not(".disabled").click(function() {
var el = $(this);
el.html(\'<span id="loader"></span>\');
var nonce = $("input#voting_nonce").val();
var data = {
action: \'add_votes_options\',
nonce: nonce,
postid: \'<?php echo $post->ID; ?>,\',
ip: \'<?php echo $_SERVER[\'REMOTE_ADDR\']; ?>\'
};