Support » Developing with WordPress » Gutenberg + Custom Post Type + Custom Role = No Featured Images?

  • I’m almost 100% sure I’m doing this right and I think it’s a bug in WordPress, but before I open a tracker, I was hoping someone else had an idea.

    I created a custom post type, enabled show_in_rest, with supports set to thumbnail and map_meta_cap enabled. I created a custom role that has all of the correct capabilities to edit my custom post type and added upload_files so they can upload images. Unfortunately, the Block Editor doesn’t let the custom role see the image (loaded via REST) or even see the Feature Image panel. There appears to be a REST request to /wp-json/wp/v2/themes that fails because the custom role doesn’t have edit_posts.

    It works for Administrators and Editors. If I grant edit_posts to my custom role, it works for the custom role also. But I don’t want the custom role to edit normal posts, only my custom posts.

    Thank you for taking the time to read this. Thank you in advance for any help you might be able to provide. I’m about to give up on this functionality.

Viewing 4 replies - 1 through 4 (of 4 total)
  • It’s confusing, but if you read here https://developer.wordpress.org/reference/functions/map_meta_cap/
    you will see that the capabilities that are plural are the primitive capabilities. Those are generically named. The meta capabilities map to the primitive ones.
    So edit_custom would map to edit_posts because it is the value in the edit_post entry of the array.

    I have been looking at the capability map stuff for awhile, and I think the docs are a mixture of before and after a couple bugs were fixed. The trick is to read the code, but it is recursive. The capabilities are in an array that uses the generic post and page keywords as subscript, so the custom values are the values of the array. The case statement is checking for the known keywords, so that it works for any custom post type, although you can write a filter to handle your own differently.

    Hi @joyously! Thank you for responding. It sounds like you are saying that the Custom Post Type’s capabilities get mapped to primitive capabilities. Which of the following are you suggesting:

    1. Modify the custom post type to set map_meta_cap to false
    2. Modify the custom role’s capabilities to different ones
    3. That everything should be working but the documentation needs updates

    Maybe a better question would be to explain my thought process and then ask which step is where I’m misunderstanding. Right now, I believe the problem is:

    1. I’ve created a custom post type example
    2. The custom post type has show_in_rest set to true
    3. The custom post type has map_meta_cap set to true
    4. The custom post type has supports with thumbnail
    5. I’ve created a custom role with capabilities:
    • read
    • edit_examples
    • edit_others_examples
    • edit_published_examples
    • publish_examples
    • upload_files
    • My example user’s role is set to the custom role
    • They go to edit a custom post of type example using the Block Editor
    • The Featured Image panel in the Document sidebar does not display
    • I notice a failed request in the browser’s Network tool to /wp-json/wp/v2/themes?status=active&_locale=user
    • I check the WP_REST_Themes_Controller::get_items_permissions_check() method and see current_user_can( 'edit_posts' )
    • I locally modify it to current_user_can( 'read' ) and now the Feature Image panel shows up, but the image does not load
    • I notice a failed request in the browser’s Network tool to /wp-json/wp/v2/media/93?context=edit&_locale=user
    • I check the WP_REST_Posts_Controller::get_item_permissions_check() method and see 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post )
    • I compare the method comment “Checks if a given request has access to read a post.” to the current behavior and decide to comment out the “can they update it” check.
    • The images now load and display in the interface.
    • I did some digging to see where the context=edit was coming from: https://github.com/WordPress/gutenberg/blob/bb0529f59746e67dae47077bf91833e42a9081c5/packages/core-data/src/resolvers.js#L56

      If the purpose of the context parameter is to mean “I’m coming from the editor, so please format the data the way I need it”, I somewhat understand. If the purpose of the context parameter is the same way that the code in the WP_REST_Posts_Controller is using it – specifically, using context as the difference between “I want to update this post” and “I want to get information about this post”, then it seems wrong to hard-code the value to “edit”.

      Am I on the right track?

    • This reply was modified 11 months, 3 weeks ago by Jonathan Champ. Reason: fix spelling of Themes controller function

    Am I on the right track?

    Sort of.
    You don’t mention what the custom post type has for capability_type. As one CPT plugin phrases it: “The post type to use for checking read, edit, and delete capabilities.” I think the default is post, but if you use your CPT, you will probably need to write a filter for map_meta_cap.

    And since you didn’t mention the code for assigning the role, I don’t know if that is correct. You can use the View Admin As plugin to help test.

    I don’t think you have to dig into the details of how the capability is checked in the controller. You should just have to get your definitions correct.

    This is an old article, but it shows how to map the meta capabilities for when you specify custom capabilities. (When you use map_meta_cap set to true, the array is filled just like shown in the code, so it’s not a great example of capabilities.)

    And if it’s still not making sense, open an issue on the editor here: https://github.com/WordPress/gutenberg/issues/

    Thank you @joyously – indeed I forgot to mention that capability_type is set to example. The role was assigned to the user manually in the wp-admin interface by my administrator account (no custom code was used to assign the role).

    With the additional information you provided I was able to craft appropriate searches to get relevant results. It looks like there are several people with similar issues:

    Thank you for your help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Gutenberg + Custom Post Type + Custom Role = No Featured Images?’ is closed to new replies.