Redirect Custom Admin Role in WordPress
As a WordPress website owner, you may want to restrict access to the WordPress admin area for certain user roles. This can be particularly useful when you have a custom admin role that you want to limit to specific tasks or pages. In this article, we'll explore how you can use the user_has_cap
WordPress filter to redirect users with a custom admin role to a specific page, effectively preventing them from accessing the WordPress admin area.
Understanding the user_has_cap
Filter
The user_has_cap
filter is a powerful WordPress hook that allows you to modify the capabilities of a user. This filter is called whenever WordPress checks whether a user has a specific capability, such as edit_posts
or activate_plugins
.
The user_has_cap
filter receives three arguments:
$allcaps
: an associative array of all the capabilities the user has.
$cap
: the capability being checked.
$args
: an array containing additional information about the capability being checked.
By using this filter, you can add custom logic to determine whether a user should have a specific capability or not. In our case, we'll use it to redirect users with a custom admin role to a specific page.
Creating the Redirect Function
Let's create a function that will check the user's role and, if it matches our custom admin role, redirect the user to a specific page. Here's an example:
function redirect_custom_admin_role( $allcaps, $cap, $args ) {
// Check if the capability being checked is 'edit_posts' and the user is in the admin area
if ( $cap === 'edit_posts' && is_admin() ) {
// Get the current user's role
$user = wp_get_current_user();
$roles = $user->roles;
// Check if the user has the custom admin role
if ( in_array( 'custom_admin_role', $roles ) ) {
// Redirect the user to a specific page
wp_redirect( 'https://example.com/redirect-page' );
exit;
}
}
// Return the original capabilities
return $allcaps;
}
add_filter( 'user_has_cap', 'redirect_custom_admin_role', 10, 3 );
Let's break down the code:
- The
redirect_custom_admin_role
function is hooked to the user_has_cap
filter.
- The function checks if the capability being checked is
'edit_posts'
and the user is in the admin area. This is a common way to check for the admin area, as the 'edit_posts'
capability is typically required for most admin actions.
- If the conditions are met, the function gets the current user's roles and checks if the
'custom_admin_role'
is in the list of roles.
- If the user has the custom admin role, the function redirects the user to a specific page using the
wp_redirect
function and then exits.
- If the user doesn't have the custom admin role, the function simply returns the original capabilities.
Customizing the Redirect Page
In the example above, the user is redirected to 'https://example.com/redirect-page'
. You can customize this URL to point to a specific page or a custom-built page that you've created for this purpose.
For example, you might want to create a page that explains to the user why they are being redirected and what they can do next. This can provide a better user experience than simply redirecting them without any context.
Considerations and Limitations
Keep in mind that this approach will only work for users who have the 'edit_posts'
capability, which is typically required for most admin actions. If you need to redirect users with other capabilities, you'll need to modify the conditions in the redirect_custom_admin_role
function accordingly.
Additionally, this solution will only work for the WordPress admin area. If you have a custom front-end area or other areas of your WordPress site that you want to restrict access to, you'll need to implement additional checks and redirects.
Conclusion
By using the user_has_cap
filter, you can easily redirect users with a custom admin role to a specific page, preventing them from accessing the WordPress admin area. This can be a useful feature for your WordPress website, especially if you have a custom admin role with specific responsibilities.
Remember to thoroughly test your implementation and consider the potential impact on your website's user experience. If you have any questions or need further assistance, feel free to reach out to the team at Flowpoint.ai, who can help you identify and fix any technical issues that may be impacting your website's conversion rates.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.