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);

3 Answers
3

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.

Leave a Reply

Your email address will not be published. Required fields are marked *