阻止用户编辑其他帖子

时间:2014-01-09 作者:raam86

我已经声明了一个新的post类型,它在管理菜单上可见:function create\\u post\\u type(){

    register_post_type( \'employer_fm\',
      array(
        \'labels\' => array(
      \'name\' => _x(\'Employer\', \'Employers\'),
      \'singular_name\' => _x(\'Employer \', \'domain\'),
    ),
        \'public\' => true, 
        \'capability_type\'    => array(\'employer\',\'employers\',\'adminstrator\'),

        \'supports\' => array(\'title\',\'author\',\'thumbnail\',\'page-attributes\'),
        \'capabilities\' => array(
              \'read_post\' => \'read_employer\',
              \'publish_posts\' => \'publish_employer\',
              \'edit_posts\' => \'edit_employers\',
              \'edit_others_posts\' => \'edit_others_employer\',
              \'delete_posts\' => \'delete_employer\',
              \'delete_others_posts\' => \'delete_others_employer\',
              \'edit_others_pages\' => \'edit_others_employer_page\',
              \'read_private_posts\' => \'read_private_employer\',
              \'edit_post\' => \'edit_employer\',
              \'delete_post\' => \'delete_employer\',
             ),
      )
    );
  }
我还声明了角色:

$result = add_role(
        \'employer\',
        __( \'Employer\' ),
        array( 
            \'edit_employer\'   => true,
            \'edit_employers\' => true,
            \'publish_employer\' => true,
            \'read_employer\' => true,
            \'delete_employer\'=> true,
            \'edit_others_employer\' => false,
            \'delete_others_employer\' => false,
            \'edit_others_employer_page\' => false
        )
    ); 
     $role = get_role( \'administrator\' );
     $role->add_cap( \'edit_employer\' ); 
     $role->add_cap( \'read_employer\' ); 
     $role->add_cap( \'delete_employer\' ); 
     $role->add_cap( \'edit_employers\' ); 
     $role->add_cap( \'publish_employer\' ); 
如果\'map_meta_cap\' => true, 用户只能查看和发布帖子(包括管理员),如果未设置,则所有用户都可以编辑所有雇主类型的帖子。

如何预防employer 编辑其他帖子的用户类型?

2 个回复
SO网友:bueltge

一种可能性是,您可以通过插件添加一个过滤器,在那里检查权限,并且只有在每篇文章中具有其用户ID的用户才能更改此文章。

下面的示例就是这样做的,但如果您只允许在CPT上使用此选项,则必须加强对自定义帖子类型的检查。

/**
 * Plugin Name: Display only own posts
 */

add_filter( \'pre_get_posts\', \'fb_pre_get_posts\' );
function fb_pre_get_posts( $queryobj ) {

    if ( ! current_user_can( \'edit_users\' ) )
        $queryobj->query_vars[ \'author\' ] = get_current_user_id();

    return $queryobj;
} 

SO网友:raam86

发布的帖子有一个单独的功能:

 $role->add_cap( \'edit_published_employers\' );

结束

相关推荐