前端AJAX的问题,在访问wp-admin/admin-ajax.php时获得302重定向

时间:2018-04-13 作者:Gauravbhai Daxini

我想对前端登录用户使用wp-ajax,但显示发现的302错误并调用主页。它对于管理员用户和未登录用户都可以正常工作。

以下是网络日志截图:

enter image description here

我使用了以下两种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();
}

1 个回复
最合适的回答,由SO网友:Gauravbhai Daxini 整理而成

下面的函数通过添加!defined(\'DOING_AJAX\') 状况良好。

function custom_blockusers_init() {
  if ( is_admin() && !defined(\'DOING_AJAX\') && ( 
  current_user_can(\'usercrp\') || current_user_can(\'userpcp\') ||  
  current_user_can(\'subscriber\') || current_user_can(\'contributor\') || 
  current_user_can(\'editor\'))) {
    session_destroy();
    wp_logout();
    wp_redirect( home_url() );
   exit;
  }
 }
add_action( \'init\', \'custom_blockusers_init\' );

结束

相关推荐

插件设置页面中的AJAX返回400个错误请求

我读了很多问题,并从抄本上复制了我的代码,但仍然没有成功。当我点击提交按钮时,我收到以下错误消息:我的php函数和JS如下所示:function enqueue_admin_settings_js($hook) { global $bblm_settings; if ( $hook != $bblm_settings ) return; wp_enqueue_script(\'admin_settings_js\', plugin_d