起草帖子和发布帖子管理

时间:2017-02-02 作者:Rosanna La Malfa

在自定义角色发布文章后,是否可能阻止他将其放回草稿中(只有管理员可以这样做)?代码是什么?谢谢你的帮助!

1 个回复
SO网友:nibnut

当然可以-您可以使用wp_insert_post_data filter:

function no_going_back_to_draft($slashed_data , $post_data) {
    if($slashed_data[\'post_status\'] == \'draft\') { // we\'re only interested in posts being saved as "drafts"
        $user = wp_get_current_user();
        if ( in_array( \'no-going-back-to-draft-role\', (array) $user->roles ) ) { // we\'re only interested in users with that special, restricted role
            $post = get_post($post_data[\'ID\']);
            if(
                $post && // if $post == null, it\'s a new post, so it\'s definitely never been published yet...
                ($post->post_status != \'draft\') // so *any other status* but draft can\'t be brought back to "draft" by this partuclar user
            ) {
                $slashed_data[\'post_status\'] = $post->post_status; // override the post\'s status: set it to the current value in the db
            }
        }
    }
    return $slashed_data;
}
add_action( \'wp_insert_post_data\', \'no_going_back_to_draft\', 10, 2 );
希望这有帮助!

相关推荐

Send email only upon draft

我正在尝试仅在将帖子保存为草稿时发送电子邮件,这似乎与当前代码不兼容: add_action( \'save_post\', \'er_send_email_on_post_draft_save\' ); function er_send_email_on_post_draft_save( $post_id ) { //verify post is not a revision if ( $post_id->post_status