我想对前端登录用户使用wp-ajax,但显示发现的302错误并调用主页。它对于管理员用户和未登录用户都可以正常工作。
以下是网络日志截图:
我使用了以下两种ajax功能:
add_action(\'wp_ajax_add_sale\', \'add_sale\');
add_action(\'wp_ajax_nopriv_add_sale\', \'add_sale\');
add_sale()
以下内容:
<?php
function add_sale() {
die("hello");
$salevehicleid = $_POST[\'salevehicleid\'];
$saleuserhidid = $_POST[\'saleuserhidid\'];
$saleusername = $_POST[\'saleusername\'];
$saleuseremail = $_POST[\'saleuseremail\'];
$saledate = date(\'Y-m-d H:i:s\');
$letsaleplaced = true;
$response = array();
// $saleuserhidid = (int)0;
// if(is_user_logged_in()){
// $user_id = get_current_user_id();
// if ($user_id == 0) {
// $saleuserhidid = (int)0;
// } else {
// $saleuserhidid = (int)$user_id;
// }
// }else{
// $saleuserhidid = (int)0;
// }
// echo $saleuserhidid;
if($saleuserhidid == (int)0){
global $wp_query;
$args = array(
\'post_type\'=>\'my_custom_sale\',
\'post_status\'=>\'publish\',
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'vehicleid\',
\'value\' => $salevehicleid,
\'compare\' => \'==\'
),
array(
\'key\' => \'usermail\',
\'value\' => $saleuseremail,
\'compare\' => \'==\'
)
)
);
$wp_query = new WP_Query( $args );
global $post;
if ( $wp_query->have_posts() ) :
$letsaleplaced = false;
endif;
}else{
global $wp_query;
$args = array(
\'post_type\'=>\'my_custom_sale\',
\'post_status\'=>\'publish\',
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'vehicleid\',
\'value\' => $salevehicleid,
\'compare\' => \'==\'
),
array(
\'key\' => \'usermail\',
\'value\' => $saleuseremail,
\'compare\' => \'==\'
),
array(
\'key\' => \'userid\',
\'value\' => $saleuserhidid,
\'compare\' => \'==\'
)
)
);
$wp_query = new WP_Query( $args );
global $post;
if ( $wp_query->have_posts() ) :
$letsaleplaced = false;
endif;
}
if($letsaleplaced){
$title = "sale";
$post_type = \'my_custom_sale\';
$new_sale = array(
\'post_title\' => $title,
\'post_status\' => \'publish\',
\'post_type\' => $post_type,
array(
\'public\' => true,
\'capability_type\' => $post_type,
\'capabilities\' => array(
\'publish_posts\' => \'publish_\'.$post_type,
\'edit_posts\' => \'edit_\'.$post_type.\'s\',
\'delete_posts\' => \'delete_\'.$post_type,
\'edit_post\' => \'edit_\'.$post_type,
\'delete_post\' => \'delete_\'.$post_type,
\'read_post\' => \'read_\'.$post_type,
\'map_meta_cap\' => true
)
)
);
$pid = wp_insert_post($new_sale);
add_post_meta($pid, \'userid\', $saleuserhidid, true);
add_post_meta($pid, \'usermail\', $saleuseremail, true);
add_post_meta($pid, \'username\', $saleusername, true);
add_post_meta($pid, \'vehicleid\', $salevehicleid, true);
add_post_meta($pid, \'saledate\', $saledate, true);
//--owner mailing--//
$owneremailid = "";
$didsubject = "New Buy Request Added";
$finalname = "";
$vehicledata = get_post_meta( $salevehicleid );
if(!empty($vehicledata)){
$usersdata = get_user_by( \'id\', $vehicledata[\'customer\'][0]);
$owneremailid = $usersdata->data->user_email;
$usernamedata = get_userdata($vehicledata[\'customer\'][0]);
if(!empty($usernamedata)){
if($usernamedata->first_name != \'\' && $usernamedata->last_name != \'\'){
$finalname = $usernamedata->first_name." ".$usernamedata->last_name;
}else{
$finalname = $usersdata->data->user_login;
}
}else{
$finalname = $usersdata->data->user_login;
}
}
$carlink = home_url().\'/view-vehicle/\'.$salevehicleid;
$content = "<div style=\'text-align:left;\'>Hi, ".ucwords($saleusername)." is interested in buying your <a href=\'".$carlink."\'>vehicle</a>.</div><br>
<div style=\'text-align:left;\'>Please contact : Using this email address ".$saleuseremail." to arrange a viewing.</div>";
$header = "Content-Type: text/html; charset=ISO-8859-1\\r\\n";
if(!empty($owneremailid)){
wp_mail($owneremailid, $didsubject, $content, $header);
}
$adminemailid = get_bloginfo(\'admin_email\');
$didsubject = "New Buy Request Added";
$carlink = home_url().\'/view-vehicle/\'.$salevehicleid;
$admincontent = "<div style=\'text-align:left;\'>".ucwords($saleusername)." place a buy request to ".ucwords($finalname)." <a href=\'".$carlink."\'>vehicle</a> and email address is ".$saleuseremail."</div>";
$header = "Content-Type: text/html; charset=ISO-8859-1\\r\\n";
if(!empty($adminemailid)){
wp_mail($adminemailid, $didsubject, $admincontent, $header);
}
//--admin mailing--//
$response[\'status\'] = true;
$response[\'bidid\'] = $pid;
$response[\'message\'] = \'Buy request added successfully\';
}else{
$response[\'status\'] = false;
$response[\'bidid\'] = 0;
$response[\'message\'] = \'This user can not send buy request.\';
}
echo json_encode($response);
die();
}