Home  >  Article  >  CMS Tutorial  >  How to restrict users in wordpress

How to restrict users in wordpress

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-07-18 15:00:593265browse

How to restrict users in wordpress

After the new user registers and logs in successfully, he will jump directly to the homepage of the WordPress website. However, after logging in, new users can still enter the backend of the WordPress website if they enter the address of the WordPress backend in the browser address bar. This is not what we want. Our requirement is that as long as new users are not the administrator of the WordPress website, they will not be allowed to enter the backend of the WordPress website. So, how can we prevent non-admin users from entering the WordPress backend?

If we want to restrict non-administrators from entering the backend of the WordPress website, we only need to compare the information of the currently logged in user with the information of the WordPress administrator user. If the comparison is unsuccessful, he will not be allowed to enter. Go into the background. If the comparison is successful, let him go into the background. It's that simple.

Related recommendations: "WordPress Tutorial"

Put the following code into the functions.php file of the wordpress theme:

//非管理员不允许进入后台
if ( is_admin() && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) ) {
$current_user = wp_get_current_user(); //获取当前登录用户的信息
if($current_user->roles[0] == get_option('default_role')) { //如果不是管理
wp_safe_redirect( home_url() ); //就安全地重定向到网站的首页
exit();
}
}

Through the above We have implemented this code (restricting non-admin users from entering the backend of the WordPress website).

The above is the detailed content of How to restrict users in wordpress. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn