Edit Flow

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; 
}
Exit mobile version