I have a front end form that allows logged in users to submit data.
I have a dropdown that grabs data from another post type.
That dropdown is giving me a “Loading error…” when attempting to use it if I am logged in as a user instead of an admin.
I tracked it down to a code snippet I added to prevent non-admins from accessing the back end of the site. It appears that it’s also preventing AJAX calls from non-admins.
Does anyone have a similar code snippet that works with AJAX or know how I can detect an AJAX call and allow it?
Okay, I added a check for wp_doing_ajax() and I no longer get “Loading error…”. I now get “No matches found” when logged in as a user. It works fine when I’m logged in as an admin.
Here is my code:
add_action( 'init', 'am_block_user_from_admin' );
function am_block_user_from_admin() {
if ( wp_doing_ajax() )
exit;
if ( is_admin() && ! current_user_can( 'administrator' ) ) {
wp_redirect( home_url() );
exit;
}
}