• mikylucky

    (@mikylucky)


    Hi there,
    I’m trying to register some new REST fields for users basing on their role.
    At the moment I’m using the register_rest_field() function, but it allows only to add a field to all the user.
    Is it possible to register a field only to users with a specific role?

    E.g.
    I want that users with admin role have a field that contributors have not.

    Thanks for the help!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Added fields are managed dynamically, there’s no persistent storage AFAIK, so you can wrap register_rest_field() in a conditional based on current_user_can(). Specify whatever admin capability that makes the most sense.

    Additionally, the callbacks you register must also confirm the user has the same capability. Similar to core callbacks like WP_REST_Meta_Fields::update_meta_value() do.

Viewing 1 replies (of 1 total)
  • The topic ‘REST API register new fields for specific users’ is closed to new replies.