WordPress is a powerful content management system that allows you to create and manage different user roles with different levels of access. In some cases, you may need to create a custom user role with specific capabilities and restrict access to certain content types. In this tutorial, we will guide you through creating a custom user role named “HR” and restricting access to a custom post type named “Career” (custom user role wordpress).
Table of Contents
Step 1: Add the HR User Role
To get started, open your theme’s functions.php
file or create a custom plugin. Add the following code to create the “HR” user role:
// Add a new user role called "hr"
function add_hr_role() {
add_role('hr', __('HR'), array(
'read' => true,
'edit_posts' => false,
'delete_posts' => false,
'publish_posts' => false,
'upload_files' => true,
));
}
add_action('init', 'add_hr_role');
This code uses the add_role
function to create a new user role named “HR” with limited capabilities. Users with this role will be able to upload files but won’t have access to editing, deleting, or publishing regular posts.
Step 2: Restrict Access to the Career Custom Post Type
Now, let’s restrict the “HR” role to access only the “Career” custom post type. Add the following code to your functions.php
file or custom plugin:
// Restrict "hr" role to access only the "career" custom post type
function restrict_hr_role() {
$role = get_role('hr');
// Add capabilities to access only the "career" custom post type
$role->add_cap('read_career');
$role->add_cap('read_private_careers');
$role->add_cap('edit_career');
$role->add_cap('edit_careers');
$role->add_cap('edit_others_careers');
$role->add_cap('edit_published_careers');
$role->add_cap('publish_careers');
$role->add_cap('delete_career');
$role->add_cap('delete_careers');
$role->add_cap('delete_others_careers');
$role->add_cap('delete_private_careers');
$role->add_cap('delete_published_careers');
}
add_action('init', 'restrict_hr_role');
This code adds specific capabilities to the “HR” role, allowing them to read, edit, publish, and delete only the “Career” custom post type.
Conclusion
By following these steps, you’ve successfully created a custom user role in WordPress named “HR” and restricted access to a specific custom post type (“Career”). This approach provides a level of granularity in managing user roles and permissions, ensuring that users with the “HR” role have access only to the content they need.
Remember to customize the code according to your specific custom post type and role requirements. Additionally, always back up your site before making significant changes to ensure a smooth development process.
I hope this article helped you to learn How to create custom user role wordpress. If you have any doubts or problem with the code, comment below to find the solutions. Also share this blog if you find this useful.
Want to build professional website for your Business or Store, Get a free quote here
Click here to get Premium Plugins and Themes at rs.249. Get 20% Off on your first order “WELCOME20”