如果管理员已修改,则阻止作者更改其帖子

时间:2014-11-16 作者:aye

在wordpress中,默认情况下,作者可以创建帖子并对其进行所有更改。

我正在寻找一种方法,如果管理员对某个帖子进行了任何修改(在管理员单击“更新”按钮后),可以限制他们对帖子进行任何更改。

对于管理员尚未修改的帖子,作者仍然可以进行任何更改。

我尝试了许多用户角色插件,但找不到任何有帮助的插件。

非常感谢你。

1 个回复
最合适的回答,由SO网友:birgire 整理而成

您可以尝试以下操作:

/**
 * Post Update Locker For Authors
 * If an administrator has updated the post, then lock it for author updates.
 * @see http://wordpress.stackexchange.com/a/168578/26350
 */

add_action( \'pre_post_update\', function( $post_ID, $data ) {

    // Target only authors:
    if( ! current_user_can( \'edit_post\' ) || current_user_can( \'edit_others_posts\' ) )      
        return;

    // Target only \'post\' post types:
    if( get_post_type( $post_ID ) !== \'post\' )
        return;

    // Fetch all administrators:
    $admins_ids = get_users( 
        array( 
            \'role\'   => \'administrator\', 
            \'fields\' => \'ID\' 
        ) 
    );

    // or hardcoded if needed:
    // $admins_ids = array( 1 );

    // Check if administrators have modified the current post, by checking the revisions:
    $posts = get_posts( 
        array( 
            \'no_found_rows\'             => true,
            \'update_post_meta_cache\'    => false,
            \'update_post_term_cache\'    => false,
            \'posts_per_page\'            => -1, 
            \'fields\'                    => \'ids\', 
            \'post_parent\'               => $post_ID, 
            \'post_type\'                 => \'revision\', 
            \'post_status\'               => \'any\', 
            \'author__in \'               => $admin_ids,  
        ) 
    );

    // Halt if an administrator has modified the post:
    if( count( $posts ) > 0 )
        wp_die( __( "Sorry, you can\'t modify this post, it\'s already been modified by an administrator! " ) );

}, 10, 2 );
您可能需要对此进行调整并进行进一步测试。

更新:

下面是一个如何删除submitdiv 无法更新当前帖子的作者的metabox:

/**
 * Hide the publish metabox for authors if an administrator has updated the post.
 * @see http://wordpress.stackexchange.com/a/168578/26350
 */

add_action( \'admin_menu\', function() {
    add_action( \'load-post.php\', \'wpse_author_update_locking\' );
});

function wpse_author_update_locking()
{
    // Setup - Modify this to your needs:
    $admin_ids = array( 1 );
    $cpt       = \'post\';

    // User input:
    $_pid    = filter_input( INPUT_GET, \'post\',   FILTER_SANITIZE_NUMBER_INT );

    // Target only $cpt post types:
    if( get_post_type( $_pid ) !== $cpt )
        return;

    // Target only authors:
    if( ! current_user_can( \'edit_post\' ) || current_user_can( \'edit_others_posts\' ) )      
        return;

    if( $_pid > 0 )
    {
        // Check if administrators have modified the current post, 
        // by checking the revisions:
        $posts = get_posts( 
            array( 
                \'no_found_rows\'             => true,
                \'update_post_meta_cache\'    => false,
                \'update_post_term_cache\'    => false,
                \'posts_per_page\'            => -1, 
                \'fields\'                    => \'ids\', 
                \'post_parent\'               => $_pid, 
                \'post_type\'                 => \'revision\', 
                \'post_status\'               => \'any\', 
                \'author__in \'               => $admin_ids,  
            ) 
        );

        // Halt if an administrator has modified the post:
        if( count( $posts ) > 0 )
        {
            remove_meta_box( \'submitdiv\', $cpt , \'side\' ); 
            add_meta_box( 
                \'submitdivmod\', 
                __( \'Publish\' ), 
                \'wpse_post_submit_meta_box\', 
                $cpt, 
                \'side\', 
                \'core\' 
            ); 
        }
    }
}

function wpse_post_submit_meta_box()
{
    _e("Sorry, you can\'t modify this post, it\'s already been modified by an administrator!");
}
然后,代替此元数据库:

The publish metabox available

作者认为:

The publish metabox NOT available

结束