2017-01-22 17 views
1

これは私のカスタムポストタイプのコードなので、ユーザの追加、編集、削除権限を与えたいと思います。ワードプレスのサブスクライバユーザにカスタムポストタイプ(追加、編集、削除)を許可する方法

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 
+0

http://wordpress.stackexchange.com/questions/14553/allow-member-to-have-access-to-custom-post-type-only-permission-to-only-edit-th –

答えて

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を参照してください。

+0

ありがとうPurvik、しかし、この関数でカスタム投稿の型名を指定する場所はどこですか? –

+0

@rarthemeカスタム投稿の型名を追加する必要はありません。これは 'capability_type'のみ必要です。あなたはすでにこの '' capability_type '=>' post '、 '。 – purvik7373

+0

@ purvik7373私は、私のカスタム投稿を追加/編集/削除するためにサブスクライバのユーザ役割をaloowingしたいと思っています。私は上記の手順に従った。しかし、それでも私にはエラーが表示されます:申し訳ありませんが、私はサブスクライバとしてサインインしているときにこのユーザーとして投稿を作成することはできません – Himani

関連する問題