Improving ‘Contributor’ features in WordPress via functions.php

Contributor posting on WordPress

If you work with an in-house copywriter or an external freelance writer for your website, you likely set up a WordPress account for them so they can login and upload their content without having full access to your website. The ‘contributor’ user role is there for that. It allows the user to create new posts and submit them for another user with ‘Editor’ or ‘Administrator’ role to review and publish. The post, once published, cannot be edited by the original writer.

Out of the box, WordPress supports this type of user and provides basics functionalities for content upload and moderation/review before publishing. You would probably like to have more advanced features though so here are a few examples of custom code improving your experience as a website owner.

Before implementing any custom functions, please note that depending on your case, the ‘Author’ role may be more appropriate to use than the ‘Contributor’ role. The difference being that the ‘Author’ can upload media and manage their own posts. The can publish and delete their posts. If your writer is trusted and fully capable of uploading all content appropriately, you may want to assign him or her the ‘Author’ role.

Please always make sure to backup your site before making changes to your theme (or child theme).

Get notified when a contributor sends a new post/article for review

Add this code to your function.php in order to send an email to multiple users when a post status changes to ‘pending’. The email will contain a link to the post. Depending on your needs, this function can be edited to send a notification based on other post status change.

// Notify relevant users when a new post is ready for review
add_action( 'transition_post_status',  function ( $new_status, $old_status, $post )
{
    if ( $new_status == 'pending' ) {
    wp_mail( '[email protected],[email protected]', $post->post_title, 'A new post titled "' . $post->post_title . '" is ready for review at ' . get_permalink( $post->ID ) . '.' );
    }
}, 10, 3 );

Allow contributors to upload media

Add this code to your function.php in order to let contributors upload media in their posts. This function adds ‘file upload permission’ to the contributor role.

//Allow Contributors to Add Media
if ( current_user_can('contributor') && !current_user_can('upload_files') )
add_action('admin_init', 'allow_contributor_uploads');

function allow_contributor_uploads() {
  $contributor = get_role('contributor');
  $contributor->add_cap('upload_files');
}

External links for more information

WordPress support page on Roles and Capabilities.

Please feel free to share your own functions for better contributor features or comment on potential improvements in the comment section!

Leave a Reply

Your email address will not be published. It will only be used for comment moderation.
Comments will only show after moderation is completed. Usually within a few minutes.