阻止作者角色编辑其他帖子

时间:2015-04-15 作者:Martin Nilsson

我已经创建了一个自定义帖子类型“Extension”和一个角色“Extension Author”。此角色的用户只能编辑自己的扩展名。

现在,用户可以单击顶部栏中的“编辑扩展”,然后进入编辑模式。

Edit Extension Link

如何防止扩展作者进入非他/她自己的扩展的编辑视图?我在设置功能时是否遗漏了什么?

创建自定义帖子类型:

$labels = array(
    \'name\'                => _x( \'Extensions\', \'Post Type General Name\', \'text_domain\' ),
    \'singular_name\'       => _x( \'Extension\', \'Post Type Singular Name\', \'text_domain\' ),
    \'menu_name\'           => __( \'Extensions\', \'text_domain\' ),
    \'parent_item_colon\'   => __( \'Parent Extension:\', \'text_domain\' ),
    \'all_items\'           => __( \'All Extensions\', \'text_domain\' ),
    \'view_item\'           => __( \'View Extension\', \'text_domain\' ),
    \'add_new_item\'        => __( \'Add New Extension\', \'text_domain\' ),
    \'add_new\'             => __( \'Add New\', \'text_domain\' ),
    \'edit_item\'           => __( \'Edit Extension\', \'text_domain\' ),
    \'update_item\'         => __( \'Update Extension\', \'text_domain\' ),
    \'search_items\'        => __( \'Search Extension\', \'text_domain\' ),
    \'not_found\'           => __( \'No Extensions found\', \'text_domain\' ),
    \'not_found_in_trash\'  => __( \'No Extensions found in Trash\', \'text_domain\' ),
);
$rewrite = array(
    \'slug\'                => \'extension\',
    \'with_front\'          => true,
    \'pages\'               => true,
    \'feeds\'               => true,
);
$capabilities = array(
    \'edit_post\'           => \'edit_extension\',
    \'read_post\'           => \'read_extension\',
    \'delete_post\'         => \'delete_extension\',
    \'edit_posts\'          => \'edit_extensions\',
    \'edit_others_posts\'   => \'edit_others_extensions\',
    \'publish_posts\'       => \'publish_extensions\',
    \'read_private_posts\'  => \'read_private_extensions\',
);
$args = array(
    \'labels\'              => $labels,
    \'supports\'            => array( \'title\',),
    \'hierarchical\'        => false,
    \'public\'              => true,
    \'show_ui\'             => true,
    \'show_in_menu\'        => true,
    \'show_in_nav_menus\'   => true,
    \'show_in_admin_bar\'   => true,
    \'menu_position\'       => 5,
    \'menu_icon\'           => \'dashicons-admin-generic\',
    \'can_export\'          => true,
    \'has_archive\'         => true,
    \'exclude_from_search\' => false,
    \'publicly_queryable\'  => true,
    \'rewrite\'             => $rewrite,
    \'capability_type\'     => \'extension\',
    \'capabilities\' => $capabilities,
);

register_post_type( \'extension\', $args );
正在创建用户角色:

    $extension_author_role = add_role(\'extension_author\', \'Extension Author\', array(
    \'read\' => true,
    \'upload_files\' => true,
    \'edit_extension\'   => true,
    \'read_extension\'   => true,
    \'delete_extension\'   => true,
    \'edit_extensions\'   => true,
    \'edit_others_extensions\'   => false,
    \'publish_extensions\'   => true,
    \'read_private_extensions\'   => false,
    )
);

1 个回复
SO网友:Charles

使用此代码段,作者现在只能看到自己的帖子,并且只能编辑自己的帖子。现在只有管理员可以编辑所有帖子

您可以尝试将“author”替换为“extension\\u author”,看看它是否如您所愿适合您。(在functions.php或您自己的插件中添加代码段)

function to_parse_query_useronly( $wp_query ) {
    if ( strpos( $_SERVER[ \'REQUEST_URI\' ], \'/wp-admin/edit.php\' ) !== false ) {
        if ( !current_user_can( \'publish_posts\' ) ) {
            global $current_user;
            $wp_query->set( \'author\', $current_user->id );
        }
    }
}
add_filter(\'parse_query\', \'to_parse_query_useronly\' );

结束

相关推荐

PHP致命错误:无法为wp-includes/capabilities.php中的非对象调用重载函数

我在apache日志中遇到了太多以下错误。PHP Fatal error: Cannot call overloaded function for non-object in wp-includes/capabilities.php on line 1187这是函数current\\u user\\u can($capability)的内部,第1187行如下所示:$current_user = wp_get_current_user(); 我不知道问题出在哪里?