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 |2 Answers
Reset to default 0To 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)
版权声明:本文标题:wp-adminadmin-ajax.php 400 Bad request (chrome console) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736632767a1945812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_ajax_
but you also needwp_ajax_nopriv_
. As Tom mentioned, share your code and perhaps we can narrow it down. – Tony Djukic Commented Feb 29, 2024 at 15:05