4

How can I get the email adress from the users by using the REST API? I'm authenticating with nonce, and it seems to be working since I can do POST requests and change stuff. Do I have to add something to make it return all the user info?

This is my JS:

(function($) {

    var nonce       = WPsettings.nonce;
    var rest_url    = WPsettings.rest_url;

    $.ajax( {
        url: rest_url + 'users/',
        dataType: "json",
        beforeSend: function ( xhr ) {
            xhr.setRequestHeader( 'X-WP-Nonce', nonce );
        }
    } )
   .done( function ( response ) {
        console.log( response );
    } );

})(jQuery);
0

3 Answers 3

5

To add the user's email address in the REST API response, register an additional field to the user object and add the email address:

register_rest_field(
    'user',
    'user_email',
    [
        'get_callback' => static function (array $user): string {
            return get_userdata($user['id'])->user_email;
        },
    ]
);

Please note, however, that this is strongly discouraged because anyone can then see the email addresses if the code is running on a publicly accessible website.

6
  • Thanks, i will try it out! It's not a problem I think since this will be used on a intranet only accessible from a restricted IP adress.
    – Johan Dahl
    Sep 2, 2017 at 10:31
  • Okay. So you can use this method to get email address from Rest API. Please don't forget to choose best answer :) Sep 2, 2017 at 11:08
  • Great, I will accept your answer as soon as I have confirmed that it works! :)
    – Johan Dahl
    Sep 2, 2017 at 11:20
  • anyone else having issues getting this to work for retrieving email? I keep getting null for $user['email']
    – big_water
    Dec 3, 2020 at 21:39
  • 1
    @big_water, code snippet was updated to your suggestion. Mar 23, 2022 at 7:38
3

If you're having an issue getting the email using Mostafa's answer, I had better luck swapping out the email retrieval as such:

register_rest_field( 'user', 'user_email',
                        array(
                            'get_callback'    => function ( $user ) {
                                return get_userdata($user['id'])->user_email;
                            },
                            'update_callback' => null,
                            'schema'          => null,
                        )
                    );
0

I think u can return current_user email,

register_rest_field( 'user', 'user_email',
    array(
        'get_callback'    => function ( $user ) {
            return wp_get_current_user()->user_email;
        },
        'update_callback' => null,
        'schema'          => null,
    )
);
4
  • I think that would put the email address of the user that you're authenticating to the REST API as on all returned user records. Which doesn't sound right to me.
    – Rup
    Oct 22, 2020 at 8:10
  • No, It will return only the current user.. so If User A logged-in so the return data will be his email.
    – Mohammed
    Oct 22, 2020 at 10:10
  • Which API are you calling to trigger this and just get the current user? The one in the question was /wp-json/wp/v2/users/ which will return all of the users, but with my email address in every record.
    – Rup
    Oct 22, 2020 at 10:12
  • Its wp-json/wp/v2/users/me developer.wordpress.org/rest-api/reference/users/#definition-5
    – Mohammed
    Oct 22, 2020 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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