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; } }
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/' );
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')
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(); } }
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!

WordPressexcelsoverotherwebsitebuildersduetoitsflexibility,scalability,andopen-sourcenature.1)It'saversatileCMSwithextensivecustomizationoptionsviathemesandplugins.2)Itslearningcurveissteeperbutofferspowerfulcontroloncemastered.3)Performancecanbeopti

Seven Must-Have WordPress Plugins for 2025 Website Development Building a top-tier WordPress website in 2025 demands speed, responsiveness, and scalability. Achieving this efficiently often hinges on strategic plugin selection. This article highlig

WordPresscanbeusedforvariouspurposesbeyondblogging.1)E-commerce:WithWooCommerce,itcanbecomeafullonlinestore.2)Membershipsites:PluginslikeMemberPressenableexclusivecontentareas.3)Portfoliosites:ThemeslikeAstraallowstunninglayouts.Ensuretomanageplugins

Yes,WordPressisexcellentforcreatingaportfoliowebsite.1)Itoffersnumerousportfolio-specificthemeslike'Astra'foreasycustomization.2)Pluginssuchas'Elementor'enableintuitivedesign,thoughtoomanycanslowthesite.3)SEOisenhancedwithtoolslike'YoastSEO',boosting

WordPressisadvantageousovercodingawebsitefromscratchdueto:1)easeofuseandfasterdevelopment,2)flexibilityandscalability,3)strongcommunitysupport,4)built-inSEOandmarketingtools,5)cost-effectiveness,and6)regularsecurityupdates.Thesefeaturesallowforquicke

WordPressisaCMSduetoitseaseofuse,customization,usermanagement,SEO,andcommunitysupport.1)Itsimplifiescontentmanagementwithanintuitiveinterface.2)Offersextensivecustomizationthroughthemesandplugins.3)Providesrobustuserrolesandpermissions.4)EnhancesSEOa

Enable comments on your WordPress website to provide visitors with a platform to participate in discussions and share feedback. To do this, follow these steps: Enable Comments: In the dashboard, navigate to Settings > Discussions, and select the Allow Comments check box. Create a comment form: In the editor, click Add Block and search for the Comments block to add it to the content. Custom Comment Form: Customize comment blocks by setting titles, labels, placeholders, and button text. Save changes: Click Update to save the comment box and add it to the page or article.

How to copy WordPress subsites? Steps: Create a sub-site in the main site. Cloning the sub-site in the main site. Import the clone into the target location. Update the domain name (optional). Separate plugins and themes.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
