0

I had created the ajax request

add_action('wp_ajax_test_action', 'test_action');

it works fine until some point

(Maybe i installed some plugin, but i had tried uninstall it but cant solve it)

So i try to add

add_action('wp_ajax_nopriv_test_action', 'test_action');

the request work again, but I don't understand

wp_ajax_nopriv suppose to work for the user not logged in

but i am a logged in user, I tried to logged out then login again, but still need to add "wp_ajax_nopriv" to make it work.

my js code:

//test ajax
$('button#test').on('click', function(e){
    e.preventDefault();
    console.log('click');
        $.ajax({
            url: $ajax_url,
            type: 'POST',
            data: {
                action: 'test_action',
                value: "send"
            },
            success: function(data){
                console.log( data);
            },
            error: function(data){
                console.log('error');
            }
        });

});
6
  • What’s your code that makes the request? Feb 11, 2022 at 7:47
  • added my js code
    – mrj1942
    Feb 11, 2022 at 8:14
  • Look at your action names. Feb 11, 2022 at 9:23
  • Thanks for your remind, that was my typo, i had correct it. But it work only added "wp_ajax_nopriv" even i had logged in.
    – mrj1942
    Feb 11, 2022 at 10:38
  • Is that JavaScript you shared being loaded after the page has finished loading? i.e. maybe with jQuery jQuery(document).ready(function($) {… Feb 12, 2022 at 1:50

0

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.

Browse other questions tagged or ask your own question.