Code Sample: Filtering Custom Statuses by Role

We’ve received a number of requests from Edit Flow users asking for the ability to limit statuses to specific roles. While this isn’t possible via the WordPress admin, you can write a bit of code to implement this. Just add the following snippet to the functions.php file of your active theme and modify as necessary.

The code sample limits authors to only set ‘Draft’ and ‘Pitch’ statuses, whereas editors (and administrators, by omission) have access to all statuses.

 -1,
	'author' => array( 'draft', 'pitch' )
);

add_filter( 'ef_custom_status_list', 'ef_x_filter_statuses_by_role', 10, 2 );

function ef_x_filter_statuses_by_role( $statuses, $post ) {
	global $role_status_map;
	
	$role = ef_x_get_user_role();
	if( isset( $role_status_map[$role] ) && $role_status_map[$role] != -1 ) {
		$statuses = ef_x_filter_by_value( $statuses, 'slug', $role_status_map[$role] );
	}
	
	return $statuses;
}

function ef_x_get_user_role() {
	$user = wp_get_current_user();
	return array_shift( $user->roles );
}

function ef_x_filter_by_value( $array, $property, $value ) { 
	$filtered_array = array();
	foreach( (array) $array as $index => $item ) { 
		if( isset( $item->$property ) ) {
			if ( ( is_array( $value ) && in_array( $item->$property, $value ) ) || $item->$property == $value ) {
				$filtered_array[$index] = $item;
			}
		}
	} 
	return $filtered_array; 
}

Comments

  1. Hi there

    Great idea. Should maybe find its way into the settings panel of the plugin.

    Something that goes into the same direction: The biggest selling point for this edit flow plugin would be if I could use it to walk a middle ground between greenlighting all authors’ contributions myself, and letting them publish at will.
    Option a: I could assign them a permission like “can publish max. X posts / day” or “can publish a post if there are < X posts published or scheduled for that day already". That is to prevent contributors from flooding my publication
    Option b: Easy email moderation. If a post reaches "pending review" status, I get an email, that shows 1) the text 2) the title 3) which tags 4) which category 5) if a featured image is set and for which date/time it is indended to go online. I can then moderate it by replying in the subject line with publish, or send it to the author with my comments. The point would be that I as the chief editor have a minimum of work to do, and I should be able to moderate without having to find a device from which I can log into the WP Dashboard

  2. hi,
    This functionality is exactly what I need, but I added this code and it broke the site. Can you tell me how to use it? Where do I put it? I only need authors to be able to write drafts, so can I just remove the ,’pitch’ from ‘author’ => array( ‘draft’, ‘pitch’ )?

    Many thanks, and its a great plugin,
    Jonathan

Speak Your Mind

*