0

I want to use wp ajax for frontend logged in user but showing error of 302 Found and call home page. it is working fine for admin users and non logged in users.

here is network log screenshot:

enter image description here

i have used both ajax function like below:

add_action('wp_ajax_add_sale', 'add_sale');
add_action('wp_ajax_nopriv_add_sale', 'add_sale');

add_sale() below:

<?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();
}
  • 302 isn't an error. It's means there's an (intentional) redirect. What does the add_sale() function do? Please include the code. If it performs a redirect of any kind then that would explain the issue. – Jacob Peattie Apr 13 '18 at 7:47
  • did you try to see if this answer solves your problem first? – Aurovrata Apr 13 '18 at 7:50
  • @Jacob Peattie files.fm/u/w7hmd2nv here is add_sale function code – gauravbhai daxini Apr 13 '18 at 7:52
  • Please include it in the question. And not a link. The actual code. – Jacob Peattie Apr 13 '18 at 7:52
  • yes i did it please check question now – gauravbhai daxini Apr 13 '18 at 7:53
0

Below function works for me by adding !defined('DOING_AJAX') in condition.

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' );
  • Thanks mate, you made my day :-) I've got a 302 redirect and I looked for "wp_redirect" into my theme code. Finally, I added && !defined('DOING_AJAX') as well and its works :-) – Mepps Jun 20 '18 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.