【问题标题】:How to allow custom post type (add, edit, delete) for subscriber users in wordpress如何在 wordpress 中允许订阅者用户自定义帖子类型(添加、编辑、删除)
【发布时间】:2017-06-06 21:16:19
【问题描述】:

这是我的自定义帖子类型代码,我想给订阅者用户添加、编辑、删除权限。

如何在 wordpress 中允许订阅者用户自定义帖子类型(添加、编辑、删除)。请帮助我提前谢谢。

add_action( 'init', 'realestate_init' );
        function realestate_init() {
        $labels = array(
            'name'               => _x( 'Realestates', 'post type general name', 'your-plugin-textdomain' ),
            'singular_name'      => _x( 'Realestate', 'post type singular name', 'your-plugin-textdomain' ),
            'menu_name'          => _x( 'Realestates', 'admin menu', 'your-plugin-textdomain' ),
            'name_admin_bar'     => _x( 'Realestate', 'add new on admin bar', 'your-plugin-textdomain' ),
            'add_new'            => _x( 'Add New', 'realestate', 'your-plugin-textdomain' ),
            'add_new_item'       => __( 'Add New Realestate', 'your-plugin-textdomain' ),
            'new_item'           => __( 'New Realestate', 'your-plugin-textdomain' ),
            'edit_item'          => __( 'Edit Realestate', 'your-plugin-textdomain' ),
            'view_item'          => __( 'View Realestate', 'your-plugin-textdomain' ),
            'all_items'          => __( 'All Realestates', 'your-plugin-textdomain' ),
            'search_items'       => __( 'Search Realestates', 'your-plugin-textdomain' ),
            'parent_item_colon'  => __( 'Parent Realestates:', 'your-plugin-textdomain' ),
            'not_found'          => __( 'No realestates found.', 'your-plugin-textdomain' ),
            'not_found_in_trash' => __( 'No realestates found in Trash.', 'your-plugin-textdomain' )
        );
        $args = array(
            'labels'             => $labels,
            'description'        => __( 'Description.', 'your-plugin-textdomain' ),
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'rewrite'            => array( 'slug' => 'realestate' ),
            'capability_type'    => 'post',
            'has_archive'        => true,
            'hierarchical'       => false,
            'menu_position'      => null,
            'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
        );
        register_post_type( 'realestate', $args );
    }
    // End register custom post type

【问题讨论】:

标签: wordpress custom-post-type


【解决方案1】:

为了简化这个过程,让我们使用一个角色并为其分配所有所需的功能来管理(添加、编辑、发布、删除)我们的自定义帖子类型。

function add_custom_caps() {
    // gets the subscriber role
    $role = get_role( 'subscriber' );

    // This only works, because it accesses the class instance.
    // would allow the subscriber to edit others' posts for current theme only
     $role->add_cap( 'read' );
     $role->add_cap( 'read_post');
     $role->add_cap( 'read_private_post' );
     $role->add_cap( 'edit_post' );
     $role->add_cap( 'edit_others_post' );
     $role->add_cap( 'edit_published_post' );
     $role->add_cap( 'publish_post' );
     $role->add_cap( 'delete_others_post' );
     $role->add_cap( 'delete_private_post' );
     $role->add_cap( 'delete_published_post' );
}
add_action( 'admin_init', 'add_custom_caps');

这段代码相当简单。我们有一个当前角色的WP_Role 对象,然后使用WordPress add_cap() 函数将我们的新_post 功能添加到所述角色。请参阅get_role() function on the codex 以更好地了解可用的功能。

【讨论】:

  • 感谢 Purvik,但是在这个函数中在哪里指定自定义帖子类型名称?
  • @rartheme 不需要添加自定义帖子类型名称,只需要 capability_type 并且您已经提到了这个 'capability_type' => 'post',
  • @purvik7373 我也在寻找订阅用户角色来添加/编辑/删除我的自定义帖子。我按照上面定义的步骤进行操作。但它仍然向我显示一个错误:对不起,当我以订阅者身份登录时,您不允许以此用户身份创建帖子
  • 你能帮帮我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 2018-02-23
  • 2020-06-11
  • 2023-03-16
  • 1970-01-01
  • 2020-02-23
  • 2014-11-06
相关资源
最近更新 更多