Support » Developing with WordPress » Adding user email address to WP API Rest

  • Hi there,

    I need to know the email address asociated with an user id with Api rest request.

    I’m performing a get request to “/wp-json/wp/v2/users/2” which returns a lot of information about this userID = 2 but email address is not there.

    How can I add this info in the response ?
    I tried adding this to functions.php in theme folder but the value still not there.

    `register_meta( ‘user’, ‘user_email’, [ ‘show_in_rest’ => true ] );

    I also read the documentation but I still have problems.

    Thanks in advance
    Maxi

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Exposing a user’s email in the API could be considered a violation of user privacy, so it’s explicitly suppressed in the response. That’s why the usual registration approach doesn’t work. You should be able to add it back in through the ‘rest_prepare_user’ filter. Your callback is passed a response object. Use its get_data() and set_data() methods to access and modify the data before returning the modified response object.

    To help protect user privacy, I recommend doing this within a conditional that checks who/what made the request, limiting email exposure only to those with a legitimate need for the information.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.