0

There is a strange and frustrating behavior of the wordpress file admin-ajax.php, when I make an ajax request, it returns an incorrect 404 error request.

function rp_add_header() {
    if (is_single() || is_page()) { 
        // include the jquery Ajax/form validation javascript file
        wp_enqueue_script( 'ajax-script', plugin_dir_url(__FILE__).'rp-script.js', array('jquery'), 1.0 ); // jQuery will be included automatically

        // create the three Ajax variables used in your template file
        wp_localize_script( 'ajax-script', 'ajax_object', array(
                        'ajaxurl' => admin_url( 'admin-ajax.php' ),
                        'errorEmpty' => __( 'You need to select one of the options.' )
                )
        );

        add_action( 'wp_ajax_ajax_action', 'ajax_action_stuff' ); // ajax for logged in users
        add_action( 'wp_ajax_nopriv_ajax_action', 'ajax_action_stuff' ); // ajax for not logged in users


    }
}
add_action('wp_head','rp_add_header',1);



function ajax_action_stuff() {
    global $mail_report_to;
    $resp['status'] = 'error';
    $resp['errmessage'] = '';
    if (!empty($_POST['report-msg'])) {
        $report_msg = $_POST['report-msg'];
        $report_url = $_POST['posturl'];
        $subject = 'Post report['.get_option('blogname').']';
        $header = 'From: '.get_option('blogname').' <'.get_option('admin_email').'>' . "\r\n";
        $message = '
Someone has reported a post:
Report: '.$report_msg.'
Post URL: '.$report_url.'
Visitor IP: '.$_SERVER['REMOTE_ADDR'].'
Date/time: '.date('Y-m-d H:i:s');
        if ( wp_mail($mail_report_to, $subject, $message, $header) ) {
            $resp['status'] = 'success';
            $resp['errmessage'] = 'Your report is submitted, thanks.';
        } else {
            $resp['errmessage'] = 'Something went wrong, please try again later.';
        }
    } else {
        $resp['errmessage'] = 'Please select one of the options.';
    }
    header( "Content-Type: application/json" );
    echo json_encode($resp);
    exit;
}

I have removed all the plugins and the problem persists, any idea?

0
0

You're only hooking your AJAX response inside wp_head():

function rp_add_header() {
    if (is_single() || is_page()) { 
        // etc.

        add_action( 'wp_ajax_ajax_action', 'ajax_action_stuff' ); // ajax for logged in users
        add_action( 'wp_ajax_nopriv_ajax_action', 'ajax_action_stuff' ); // ajax for not logged in users


    }
}
add_action('wp_head','rp_add_header',1);

But wp_head doesn't run on admin-ajax.php, so your callbacks will never be hooked. You need to move these hooks outside of that function:

function rp_add_header() {
    // etc.
}
add_action( 'wp_head', 'rp_add_header', 1 );

function ajax_action_stuff() {
    // etc.
}
add_action( 'wp_ajax_ajax_action', 'ajax_action_stuff' ); // ajax for logged in users
add_action( 'wp_ajax_nopriv_ajax_action', 'ajax_action_stuff' ); // ajax for not logged in users

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.