WordPress is a powerful tool that allows for significant customization. In this tutorial, we’ll walk through the process of creating a custom post type, in our case called ‘Downloads’, and how to assign custom roles to manage these types. This is particularly useful for assigning specific permissions to users, limiting their actions to creating, editing, or deleting content within this custom post type.
Registering a Custom Post Type
To begin, we need to register our custom post type. This is done using the add_action
hook in WordPress. The following code needs to be added to your theme’s functions.php
file or a custom plugin.
function register_post_type() {
$labels = [
'name' => __( 'Downloads' ),
'singular_name' => __( 'Download' ),
'menu_name' => __( 'Downloads' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Download' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Download' ),
'new_item' => __( 'New Download' ),
'view' => __( 'View' ),
'view_item' => __( 'View Download' ),
'search_items' => __( 'Search Downloads' ),
'not_found' => __( 'No roles found' ),
'not_found_in_trash' => __( 'No roles found in Trash' ),
'parent' => __( 'Parent Download' ),
];
$args = [
'labels' => $labels,
'public' => true,
'supports' => [ 'title', 'editor', 'custom-fields' ],
'has_archive' => true,
'rewrite' => [ 'slug' => 'downloads' ],
'show_in_rest' => true,
'capability_type' => 'download',
];
register_post_type( 'download', $args );
}
add_action( 'init', 'register_post_type' );
Replace the label and argument placeholders with your desired settings. This example sets up a post type called ‘Downloads’ with various labels and configurations.
The argument capability_type
sets all the caps using download suffix. Is there where the caps are created and assigned to the custom post.
Next, we need to assign those caps to the Roles to which we want to grant access.
Creating a Custom Role
After setting up the custom post type, we need to create a role that will manage these downloads. This is achieved again using the add_action
hook.
function create_downloads_role() {
$capabilities = [
'read',
'edit_download',
'read_download',
'delete_download',
'edit_downloads',
'edit_others_downloads',
'publish_downloads',
'read_private_downloads',
'delete_downloads',
'delete_private_downloads',
'delete_published_downloads',
'delete_others_downloads',
'edit_private_downloads',
'edit_published_downloads',
];
add_role( 'download_manager', 'Download Manager', [] );
$roles = [ 'administrator', 'editor', 'download_manager' ];
foreach ( $roles as $role ) {
$role_to_update = get_role( $role );
foreach ( $capabilities as $cap ) {
$role_to_update->add_cap( $cap );
}
}
}
add_action( 'init', 'create_downloads_role' );
This script creates a new role called ‘Download Manager’ and assigns a specific set of capabilities to it. It also extends these capabilities to administrators and editors.
Let’s test it
Next, you can create a user and assign it the role ‘Download Manager’. Then, log in with the user and you will see that only the option to manage the custom post created will appear. The rest of the options will not be available.
Conclusion
By following these steps, you can successfully create custom post types and roles in WordPress. This allows for more controlled and specific management of content and user permissions within your website.
Feel free to adjust the code snippets according to your specific needs. Remember to test any changes in a development environment before applying them to your live website.