admin管理员组

文章数量:1125610

I have a download button (to download a pdf file) on my website page, underneath it, I have the number of times that this button was clicked, it is updated in real time using Ajax-JQuery WP method, When I'm logged in as ADMIN to the website and display that page, everything is working perfectly, I mean at each time I click on that button, I download that file, and at the same time the number of times is updated (in the database and in the page). But, when I'm not logged into the website, and I display that page, I don't see that "number of times" (click on download button), and I get this specific error : jquery.min.js?ver=3.7.1:2 POST https://MyWebsiteUrl/wp-admin/admin-ajax.php 400 (Bad Request)

In my mind, I'm saying that this is caused by asking access for Admin-ajax from a VISITOR, how can I avoid that please ?

Thanks !

I have a download button (to download a pdf file) on my website page, underneath it, I have the number of times that this button was clicked, it is updated in real time using Ajax-JQuery WP method, When I'm logged in as ADMIN to the website and display that page, everything is working perfectly, I mean at each time I click on that button, I download that file, and at the same time the number of times is updated (in the database and in the page). But, when I'm not logged into the website, and I display that page, I don't see that "number of times" (click on download button), and I get this specific error : jquery.min.js?ver=3.7.1:2 POST https://MyWebsiteUrl/wp-admin/admin-ajax.php 400 (Bad Request)

In my mind, I'm saying that this is caused by asking access for Admin-ajax from a VISITOR, how can I avoid that please ?

Thanks !

Share Improve this question asked Feb 29, 2024 at 13:54 Sam94Sam94 151 silver badge5 bronze badges 3
  • there's no code to debug in your question, can you edit your question to include the code? Usually this issue is caused by not including an AJAX action when making the request in javascript, or including an AJAX action that doesn't exist due to a spelling mistake. Also the admin-ajax.php interface is very old, is there a reason you didn't register a modern REST API endpoint and make an AJAX query to that instead? If you'd done this with the REST API it would have told you where the problem was in plain english – Tom J Nowell Commented Feb 29, 2024 at 14:54
  • You may have only set your AJAX up using wp_ajax_ but you also need wp_ajax_nopriv_. As Tom mentioned, share your code and perhaps we can narrow it down. – Tony Djukic Commented Feb 29, 2024 at 15:05
  • 1 Thanks @TonyDjukic, wp_ajax_nopriv was the little mistake I made, I didn't know about it before. – Sam94 Commented Feb 29, 2024 at 16:09
Add a comment  | 

2 Answers 2

Reset to default 0

To simplify your process and prevent redundant conditional checks just stack the two actions on top of each other.

wp_ajax_ runs for authenticated users and thus does the conditional check of is_user_logged_in() anyway. wp_ajax_nopriv_ does the same thing, but looks for unauthenticated users.

The actions themselves run the conditions anyway, so you needn't run it again:

function get_update_clickCount() {
  //your code here
}
add_action( 'wp_ajax_get_update_clickCount', 'get_update_clickCount' );
add_action( 'wp_ajax_nopriv_get_update_clickCount', 'get_update_clickCount' );

Answer : instead of putting wp_ajax_action in my functions.php theme's file, I put this :

if ( is_user_logged_in() ){
   //when the wp_ajax_$FiredActionName action is called, we execute get_update_clickCount callback function
   add_action( 'wp_ajax_get_update_clickCount', 'get_update_clickCount' );
} else {
    //when the wp_ajax_nopriv$FiredActionName action is called, we execute get_update_clickCount callback function
    add_action( 'wp_ajax_nopriv_get_update_clickCount', 'get_update_clickCount' );
}

本文标签: wpadminadminajaxphp 400 Bad request (chrome console)