0

I've used several tutorials and tried to apply various "solutions", but I'm still unable to get a custom role to edit their own posts for a custom type.

I created the role, which shows fine...

function mymodule_create_role() {
  add_role( 'resident', 'New Role', array(

    'read' => true, // True allows that capability
    'create_posts' => false,
    'edit_posts' => false, // Allows user to edit their own posts
    'publish_posts'=>false, //Allows the user to publish, otherwise posts stays in draft mode
    'edit_published_posts'=>false,
    'upload_files'=>true,
    'delete_published_posts'=>false,
  ));
}

I've also registered my new content type, which seems fine...

function register_bulletin_board() {
  $labels = array( 
    'name' => _x( 'Bulletin Board', 'bulletin_board' ),
    'singular_name' => _x( 'Bulletin Board', 'bulletin_board' ),
    'add_new' => _x( 'Add New', 'bulletin_board' ),
    'add_new_item' => _x( 'Add New Bulletin Board', 'bulletin_board' ),
    'edit_item' => _x( 'Edit Bulletin Board', 'bulletin_board' ),
    'new_item' => _x( 'New Bulletin Board', 'bulletin_board' ),
    'view_item' => _x( 'View Bulletin Board', 'bulletin_board' ),
    'search_items' => _x( 'Search Bulletin Board', 'bulletin_board' ),
    'not_found' => _x( 'No bulletin board found', 'bulletin_board' ),
    'not_found_in_trash' => _x( 'No bulletin board found in Trash', 'bulletin_board' ),
    'parent_item_colon' => _x( 'Parent Bulletin Board:', 'bulletin_board' ),
    'menu_name' => _x( 'Bulletin Board', 'bulletin_board' ),
  );

  $args = array( 
   'labels' => $labels,
   'hierarchical' => true,
   'description' => 'Bulletin board for citizen posts',
   'supports' => array( 'title', 'editor','author'),
   'public' => true,
   'show_ui' => true,
   'show_in_menu' => true,
   'menu_icon' => get_bloginfo('template_url') . '/images/imagegallery.png',
   'show_in_nav_menus' => true,
   'publicly_queryable' => true,
   'exclude_from_search' => false,
   'has_archive' => true,
   'query_var' => true,
   'can_export' => true,
   'rewrite' => true,
   'capabilities' => array(
     'edit_post' => 'edit_bulletin_board',
     'edit_posts' => 'edit_bulletin_boards',
     'edit_others_posts' => 'edit_other_bulletin_board',
     'publish_posts' => 'publish_bulletin_board',
     'read_post' => 'read_bulletin_board',
     'read_private_posts' => 'read_private_bulletin_board',
     'delete_post' => 'delete_bulletin_board'
    ),

    'map_meta_cap' => true
  );

  register_post_type( 'bulletin_board', $args );
}

And finally, I've applied the add_cap to my roles, which includes my new "resident" role...

$roles = array('administrator','resident','editor');

// Loop through each role and assign capabilities
foreach($roles as $the_role) { 

  $admins = get_role( $the_role );
  $admins->add_cap( 'can_edit_posts' );
  $admins->add_cap( 'edit_bulletin_board' ); 
  $admins->add_cap( 'edit_bulletin_boards' ); 
  $admins->add_cap( 'edit_other_bulletin_board' ); 
  $admins->add_cap( 'publish_bulletin_board' ); 
  $admins->add_cap( 'read_bulletin_board' ); 
  $admins->add_cap( 'read_private_bulletin_board' ); 
  $admins->add_cap( 'delete_bulletin_board' ); 
}

Now, when I login as a "resident", I can post a Bulletin Board post, but this user has no ability to edit or delete. From what I've read, the capabilities should be tied via add_cap() to the capabilities array used with register_post_type(), which seems to be the case.

I've also installed the User Role Editor plugin and I see the checks for editing of the bulletin board, but that still doesn't seem to mean anything.

Any suggestions would be very appreciated.

  • I've gone another route with this. It would have been interesting to figure out, but using the Custom Post Type UI plugin (and defining the capabilities field) with the User Role Editor plugin seems to do the trick. I am still creating a Role via the module. – user1932694 Nov 15 '17 at 21:27
0

Not an answer to the question, but a workaround.

I found that using the Custom Post Type UI plugin to create the custom datatype would offer new capabilities, as long as I changed the Capability Type field.

Using the User Role Editor plugin seems to allow me to bridge the permissions with a custom user role.

0

when you fixe capabilities on the custom type registration, it gives a strange mix of capabilities. Look the result with this code :

echo "<pre>";
var_dump(get_post_type_object('bulletin_board')->cap);

I recommand to use "capability_type" like that :

$args = array(
    'labels' => $labels,
    'hierarchical' => true,
    'description' => 'Bulletin board for citizen posts',
    'supports' => array(
        'title',
        'editor',
        'author'
    ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_icon' => get_bloginfo('template_url') . '/images/imagegallery.png',
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,

    'capability_type' => "bulletin_board",
    'map_meta_cap' => TRUE,
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.