Home / Disable / Restrict Dashboard Access
Duplicate Snippet

Embed Snippet on Your Site

Restrict Dashboard Access

Limit dashboard access to users with role capacity only. Yet, allows users without role to access frontend to comment.

Code Preview
php
<?php
function restrict_dashboard_access() {
    // Allow AJAX requests to proceed
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
        return;
    }
    
    // Check if user is not an administrator
    if ( ! current_user_can( 'administrator' ) && is_admin() ) {
        wp_redirect( home_url() );
        exit;
    }
}
add_action( 'init', 'restrict_dashboard_access' );   

Comments

Add a Comment