WordPress | Create custom user role
This time i’m working on a Blog.
It’s a Blog about wine and winery, can have a lot contributors & authors, so, the customer request more roles than wordpress offer, fortunately, wordpress has ability to manage custom role.
We want create custom role and then query about them.
For create a custom role ( like the authors role ), add this snippets directly in your theme function.php :
/* Aggiunto User Blog Author */
add_role(
'blog_author', // The name of the new role
__( 'Blog Author' ), // the label of the new role
array( // The Capabilities of the new role
'read' => true, // true allows this capability
'edit_published_posts' => true,
'edit_posts' => true,
'delete_published_posts' => true,
'delete_posts' => true,
'publish_posts' => true,
'upload_files' => true,
)
);
Now we need to loop over the new role for an ” Blog Authors “page :
<?php $args = array( 'orderby' => 'display_name', 'order' => 'ASC', 'role' => 'blog_author' );
$authors = get_users( $args );
foreach ( $authors as $author ) {
$author__first_name = get_the_author_meta( 'first_name', $author->ID );
$author__last_name = get_the_author_meta( 'last_name', $author->ID );
?>
<h3 class="title"><?php echo $author__printable_name .' '. $author__printable_lastname; ?></h3>
}
That’s all, In this way, you can add and manage all roles that you ( or your customer … ) need.