0

I'm trying to perform an action with AJAX in a Child theme template in Wordpress. I've looked into many similar issues. However, I couldn't find a solution. I've been dealing with this for about 2 hours. I'll be glad if anyone can help.

I have checked the error.log file several times. However, there is no problem.I am getting the following error in the browser console:

XHRPOSThttps://example.com/wp-admin/admin-ajax.php [HTTP/3 400 Bad Request 905ms]

...themes/child-theme/process/template.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <button type="button" id="continue_process" onclick="continue_process()">Continue</button>
    <script>
        function continue_process() {
            var ajax_nonce  = '<?php echo wp_create_nonce( "before_process_nonce" ); ?>';
            var ajax_url    = '<?php echo admin_url( "admin-ajax.php" ); ?>';
            jQuery.ajax({
                url         : ajax_url, /* WordPress AJAX endpoint.*/
                method      : 'post',
                data : {
                    'action'        :'x_before_process',
                    'security'      :ajax_nonce
                },
            success : function( response ) {
                /* Get AJAX data from */
                const obj = JSON.parse(response);
                console.log(obj.message);
            },
            error : function( err ) {
                /*Error */
                console.log(err)
            }
        });
        }
    </script>
</body>
</html>

...themes/child-theme/process/process-ajax.php

<?php
if ( ! defined( 'ABSPATH' ) ){exit; /*Exit if accessed directly*/ }
add_action("wp_ajax_nopriv_x_before_process", "x_before_process");

function x_before_process() {
    if(!wp_verify_nonce( sanitize_key($_POST['security']), 'before_process_nonce' )){
        wp_send_json_error('Invalid nonce');
        die();
    }

    $message = ['type'=>'success','message'=>'True'];
    wp_send_json_success($message);
    wp_die();
}
?>

1 Answer 1

0

It has nothing to do with child themes, the problem is here:

add_action("wp_ajax_nopriv_x_before_process", "x_before_process");

Notice the nopriv, this is for logged out users, but it's likely you are logged in

This needs adding:

add_action("wp_ajax_x_before_process", "x_before_process");

Note that this would not have been an issue if it was built using a more modern AJAX API such as the WP REST API

7
  • There is no problem there. What I want is for users who are not already logged in. I found the problem. But I don't know why. If I move php code to function.php (in child theme) I don't get 400 Bad error. Why could this be? So, in process-ajax.php this will not work. Mar 28 at 15:37
  • a HTTP 400 and a response of 0 indicates no handler was found, how and where are you loading process/process-ajax.php? Note that the nopriv handler only works for logged out users, it does not work for logged in users. Also have you confirmed the nonce check is not the problem?
    – Tom J Nowell
    Mar 28 at 15:48
  • Maybe not working because of Wordpress caching system. As it said at the beginning, the HTML/JS content is here: themes/child-theme/process/template.php . Also here is the PHP content, the part where I can meet the ajax request: themes/child-theme/process/process-ajax.php . If I move this part, PHP part, here (themes/child-theme/function.php) the problem is solved. Strange. Mar 28 at 15:53
  • how is process-ajax.php being loaded though? Is it a require in the function.php file? Is the file being included in code that runs on a hook? From inside an object/function? Is it being included from a template file?
    – Tom J Nowell
    Mar 28 at 16:05
  • Yes, that's what I missed unfortunately. I thought of this now. Thank you. Do you think it makes sense to use the after_setup_theme hook? Mar 28 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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