0

I need to restrict the access to the dashboard fon non-admin. Therefore i'm using this function in the functions.php:

function ggp_restrict_dashboard_access_function() {
    if (!current_user_can('administrator')){
        wp_redirect(home_url());
    }
}
add_action('admin_init', 'ggp_restrict_dashboard_access_function', 1);

Problem is that because of this, Ajax calls are returning a 301 for non logged in users as well as for logged in non-admin users.

How can i workaround this?

1

You need to also check it's not an AJAX request inside your hook:

if ( ! current_user_can( 'administrator' ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
    wp_redirect( home_url() );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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