Home CMS Tutorial WordPress How to restrict some WordPress user roles from entering the backend

How to restrict some WordPress user roles from entering the backend

Apr 05, 2020 pm 05:10 PM
wordpress

How to restrict some WordPress user roles from entering the backend

Sometimes we want to restrict WordPress to some user roles from accessing the backend, which can be achieved through the following code.

1. Only administrator, editor and author roles are allowed to access the backend

Add the following code to the current theme function template functions.php:

add_action( 'init', 'zm_redirect_wp_admin' );
function zm_redirect_wp_admin() {
if ( is_admin() && is_user_logged_in() && !current_user_can( 'manage_options' ) && !current_user_can( 'publish_pages' ) && !current_user_can( 'publish_posts' ) && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX )  ){
wp_safe_redirect( home_url() );
exit;
}
}
Copy after login

Determine whether to log in and User roles, user roles that are prohibited from accessing the backend will jump directly to the homepage of the website.

If you need to jump to a specified page link, such as the front-end user center, you can modify the code in line 4 to be similar:

wp_safe_redirect( 'https://zmingcx.com/' );
Copy after login

Can only jump to links within the site, not to Off-site links.

If only administrators are allowed to access the backend, you can delete the code that allows editors and authors to enter the backend:

&& !current_user_can('publish_pages') && !current_user_can('publish_posts')
Copy after login

2. Prohibit default registered user roles from entering the backend

The default registered user role refers to: WordPress backend → Settings → General, set the role in the default role for new users.

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();
}
}
Copy after login

Code from: www.ludou.org

If you modify the default role for a new user, it will be invalid for users with other roles that have been registered before.

The above two pieces of code have added judgment and will not affect the front-end ajax request.

Related recommendations: "WordPress Tutorial"

The above is the detailed content of How to restrict some WordPress user roles from entering the backend. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP vs. Flutter: The best choice for mobile development PHP vs. Flutter: The best choice for mobile development May 06, 2024 pm 10:45 PM

PHP vs. Flutter: The best choice for mobile development

How to change page width in wordpress How to change page width in wordpress Apr 16, 2024 am 01:03 AM

How to change page width in wordpress

In which folder are wordpress articles located? In which folder are wordpress articles located? Apr 16, 2024 am 10:29 AM

In which folder are wordpress articles located?

How to create a product page in wordpress How to create a product page in wordpress Apr 16, 2024 am 12:39 AM

How to create a product page in wordpress

How to search for authors in WordPress How to search for authors in WordPress Apr 16, 2024 am 01:18 AM

How to search for authors in WordPress

Where is the wordpress template file? Where is the wordpress template file? Apr 16, 2024 am 11:00 AM

Where is the wordpress template file?

Which version of wordpress is stable? Which version of wordpress is stable? Apr 16, 2024 am 10:54 AM

Which version of wordpress is stable?

What language is used to develop WordPress? What language is used to develop WordPress? Apr 16, 2024 am 12:03 AM

What language is used to develop WordPress?

See all articles