How would we restrict access to the WP admin area to all users except admins?
The users on our site have their own profile pages which do all the functions they need.
So admin should be off limits to all except admins.
How to do that?
How would we restrict access to the WP admin area to all users except admins?
The users on our site have their own profile pages which do all the functions they need.
So admin should be off limits to all except admins.
How to do that?
You must be logged in to post a comment.
We can hook to the
admin_init
action and check if the user is an administrator by using thecurrent_user_can()
function to see if the current user canmanage_options
, which is something only an administrator can do.This code, when pasted into your functions.php file, will display a message when a non-admin tries to access the dashboard:
If you prefer, you can provide better user experience by redirecting the user to the home page instead:
If you want to redirect the user to their profile page, replace
home_url()
in the code above with the link.You could write a plugin and hook into
admin_init
.The codex actually gives an example with the feature you are looking for.
http://codex.wordpress.org/Plugin_API/Action_Reference/admin_init#Example:_Access_control
Some of the answers given can be fine in most situations but I think none of them warranty to do exactly what is asked because none of the answers check user roles, they check capabilities and capabilities can be assigned and removed form roles. So, to give a exact answer, the user roles must be checked, not capabilities:
If you want to check that the user has “manage_options” capability, you can. In fact, it is the best option in most cases. Although this capability is associated by default to administrator users, the capability can be removed from admin role or it can be assigned to other user roles. That is why, in most cases, checking what the user can or can not do is better than checking the user role. So, in most cases checking for capabilities should be the choosen way but you mush have this concept clear and choose the best option for your situation and purpose:
Try the Adminimize plugin.
You can lock things down pretty well with that.
You could also try setting access up through htaccess file
Put these lines in your
functions.php
Try this, never through errors in face of an end user. Against a good UX. This code redirects them to Home.
I would use WP Frontend and set it for everybody expect admins.