Howto Prevent Admin Panel Access For Logged In Users In WordPress
Sometimes you don’t want normal users access WordPress admin panel, all users’ actions can be done in the front-end. This article will show you a small tip for preventing admin panel access for logged in users in WordPress.
Paste the following code into your functions.php
file or in your plugin main file:
function prevent_admin_access()
{
if (strpos( strtolower( $_SERVER[‘REQUEST_URI’] ), ‘/wp-admin’ ) && !current_user_can( ‘administrator’ ) ){
wp_redirect( home_url() );
}
}
add_action( ‘init’, ‘prevent_admin_access’, 0 );
The script will check the request URI, if it contains /wp-admin
, and users don’t haveadministrator
role, then we redirect them to the homepage.
The default user role for admin panel access is administrator
, you can change it into any role(editor
, author
, …) or any capability (edit_dashboard
, edit_posts
, …). If users have the given role (or higher) or they have given capability, then they can access to WordPress Dashboard.
No Comments