Dotlayer
  • News
  • Startups
  • Tutorials
  • SEO
  • Marketing
  • Interviews
No Result
View All Result
Dotlayer
  • News
  • Startups
  • Tutorials
  • SEO
  • Marketing
  • Interviews
No Result
View All Result
Dotlayer
No Result
View All Result

How to Restrict WordPress Dashboard Access to Admins Only?

March 26, 2017
in Tutorials
0 0
Share on FacebookShare on Twitter

One of our clients asked how we would restrict access to the WordPress/Dashboard admin area to all other users except admins? Sometimes there are users on the site that are just regular users but not admin users. How to do that? To do this, add the code below to functions.php and all users who are not administrators and try to access the admin panel will be redirected to the homepage URL. This essentially provides a good way of keeping members away from anything WordPress on your site.

function restrict_admin_access(){
	global $current_user;
	get_currentuserinfo();
	if (!array_key_exists('administrator', $current_user->caps)) {
		wp_redirect( get_bloginfo('url') );
		exit;
	}
}
add_action('admin_init', 'restrict_admin_access', 1);

Basically, the way the code works is that, on line 2 within the function, we fetch the current user who is logged in. Once we have that in the variable, we check the session information for the current user on line 3. Every user information has attributes or properties. The properties contain the various permissions that the user has. In this case, we check to make sure the value administrator exists in the list of properties, if it doesn't we just redirect the user to the blog's homepage. This way the user is never able to access the dashboard or admin page.

ShareTweetPin
Previous Post

Understanding and Customizing the WordPress Core Database

Next Post

Why and How to Safely Disable The JSON REST API in WordPress 2017

Next Post

Why and How to Safely Disable The JSON REST API in WordPress 2017

You might also like

Calendarific Unveils New Pricing Plans

Calendarific Unveils New Pricing Plans

July 27, 2023
CurrencyBeacon vs. Currency Freaks, Fixer.io, and OpenExchangeRates: Which API is Best?

CurrencyBeacon vs. Currency Freaks, Fixer.io, and OpenExchangeRates: Which API is Best?

June 17, 2023
Mint Linux vs Ubuntu: Which is Right For You?

Mint Linux vs Ubuntu: Which is Right For You?

March 12, 2022
Net Neutrality: What is it and Why Should You Care?

Net Neutrality: What is it and Why Should You Care?

March 12, 2022
Solid State Drives – Why You Should Buy One Today

Solid State Drives – Why You Should Buy One Today

March 12, 2022

Machine Learning Algorithms Every Beginner Should Know

January 25, 2022
  • Terms of Service
  • Privacy Policy
  • Careers

© 2021 Dotlayer.com

No Result
View All Result
  • About Us
  • Advertise
  • Blog
  • Careers
  • Contact
  • Contact Us
  • Get Featured
  • Home Layout 1
  • Home Layout 2
  • Home Layout 3
  • Privacy Policy
  • Security
  • Services
  • Subscribe To Dotlayer
  • Terms of Service
  • Write For Us

© 2021 Dotlayer.com

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In