Tag Archives: WP-admin

Restrict WP Admin access

Quick tips to prevent users other than administrators to access WP-admin and the admin toolbar.
add_action( 'admin_init', 'your_method', 1 ); /* blocks WP-admin */
function your_method() {
$user = wp_get_current_user();
if ( ! $user->has_cap('administrator') && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
wp_die( __( 'You are not allowed to access the back end of this site.' ), 'Access Denied', array( 'back_link' => true ) );
}
}

add_action('set_current_user', 'your_method'); /* hides admin toolbar */
function your_method() {
$user = wp_get_current_user();
if ( !$user->has_cap('administrator') ) {
add_filter('show_admin_bar', '__return_false');
}
}

Hope this helps! Please leave me a note and let me know if there is a better way to achieve the same results.