0

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 !

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
    Feb 29 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. Feb 29 at 15:05
  • 1
    Thanks @TonyDjukic, wp_ajax_nopriv was the little mistake I made, I didn't know about it before.
    – Sam94
    Feb 29 at 16:09

2 Answers 2

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' );
1
  • 1
    Thanks @TonyDjukic, that's the correct answer.
    – Sam94
    Mar 4 at 6:36
0

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' );
}
1
  • Don't think you even need to wrap it in the condition as it the whole process behind no_priv is essentially to run the function for users who aren't logged in. So you're essentially running a conditional check twice. Almost like if( is_user_logged_in() ) { if( is_user_logged_in() ) { 'run callback' } }. After your callback function get_update_clickCount() just add both add_action() - they do the conditional check for you. Feb 29 at 20:44

Your Answer

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

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