Search code examples
wordpresswordpress-themingcustom-wordpress-pageswordpress-thesis-theme

In publish meta-box, how to make post_status conditional to certain users


I want to let custom user role 'cm' only see 'cm approved' and 'cm rejected', and'rm' only see 'cm approved', 'rm approved', 'rm rejected' in the publish meta-box. See my screenshot below, thanks!

enter image description here


Solution

  • I just found the solution after I talked to a wordpress expert in the meetup. As I am using the Edit Flow, everything becomes easy. I recommend you install that tool too.

    add_filter('ef_custom_status_list', 'custom_by_roles');
    function custom_by_roles($custom_statuses){
        $current_user = wp_get_current_user();
        $permitted_statuses = array();
        if ($current_user -> roles[0] == 'cm'){ 
            $permitted_statuses = array(
                'cm-approved',
                'cm-rejected',
                'received'
            );
        }elseif ($current_user -> roles[0] == 'rm'){
            $permitted_statuses = array(
                'cm-approved',
                'rm-approved',
                'rm-rejected'
            );
        }
        foreach($custom_statuses as $key => $custom_status){
            if(!in_array($custom_status->slug, $permitted_statuses))
            unset($custom_statuses[$key]);
        }
        return $custom_statuses;
    }