0

I'm trying to output all of the fields in the json result but seem to be using the object incorrectly. I want to add author meta fields to the post endpoint.

This doesn't work

    function slug_show_author_meta( $object, $field_name, $request ) {
      return  get_the_author_meta( $object[ 'id' ], $field_name );
    }

This does but only gives me one field

    function slug_show_author_meta( ) {
      return  get_the_author_meta('user_nicename');
    }

Ideally I'd just like to get user_nicenaem, user_email, display_name, nicename, first_name and last_name. Thanks!

3 Answers 3

4

This should work:

function wpse_20160421_get_author_meta($object, $field_name, $request) {

    $user_data = get_userdata($object['author']); // get user data from author ID.

    $array_data = (array)($user_data->data); // object to array conversion.

    $array_data['first_name'] = get_user_meta($object['author'], 'first_name', true);
    $array_data['last_name']  = get_user_meta($object['author'], 'last_name', true);

    // prevent user enumeration.
    unset($array_data['user_login']);
    unset($array_data['user_pass']);
    unset($array_data['user_activation_key']);

    return array_filter($array_data);

}

function wpse_20160421_register_author_meta_rest_field() {

    register_rest_field('post', 'author_meta', array(
        'get_callback'    => 'wpse_20160421_get_author_meta',
        'update_callback' => null,
        'schema'          => null,
    ));

}
add_action('rest_api_init', 'wpse_20160421_register_author_meta_rest_field');
4
  • tried it and gave me back post_author_meta": [0] I named the field and added it to the post. register_rest_field('post', 'post_author_meta', array( 'get_callback' => 'slug_show_author_meta', 'update_callback' => null, 'schema' => null, ) ); before I saw your code i did try get_user_data and I can get get back nicename, displayname etc but not last or first. I changed your code to include $post_author = (int) $object['author']; $user_data = get_userdata($post_author); but still returns the wrong fields
    – Mr Brimm
    Commented Apr 21, 2016 at 2:46
  • Whoops! I didn't notice you want to get post author meta :). Just updated the answer.
    – Minh Tri
    Commented Apr 21, 2016 at 2:56
  • Ha. You def pointed me in the right direction and I got it. Thanks! I tried yours and that works as well. See what I did that was similar.
    – Mr Brimm
    Commented Apr 21, 2016 at 3:22
  • Cheer! Welcome to WPSE :)
    – Minh Tri
    Commented Apr 21, 2016 at 3:29
2

This worked for me:

add_action( 'rest_api_init', 'user_puntuacio' );

function user_puntuacio() {
    register_rest_field( 'user', 'puntuacio',
        array(
            'get_callback'    => 'get_user_puntuacio',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}


function get_user_puntuacio( $object, $field_name, $request ) {
    return get_user_meta( $object[ 'id' ], 'puntuacio', true );
}

Note that 'puntuacio' is the custom user meta I wanted to add.

0

This did the trick thanks to @Dan for pointing me in the right direction.

    function slug_show_author_meta( $object ) {
       $post_author = (int) $object['author'];
        $array_data = array();

        $array_data['login'] = get_the_author_meta('login');

        $array_data['email'] = get_the_author_meta('email');

        $array_data['user_nicename'] = get_the_author_meta('user_nicename');

        $array_data['first_name'] = get_user_meta($post_author, 'first_name', true);

        $array_data['last_name'] = get_user_meta($post_author, 'last_name', true);

        $array_data['nickname'] = get_user_meta($post_author, 'nickname', true);

        return array_filter($array_data);
    }

it returns

     "post_author_meta": {
     "login": "billy"
     "email": "[email protected]"
     "user_nicename": "billy"
     "first_name": "Billy"
     "last_name": "Boy"
     "nickname": "billy"
      }
2
  • If answer works for you then please accept the answer by Dan instead of posting a thank you post!
    – Sumit
    Commented Apr 21, 2016 at 7:07
  • 1
    I got you but not really a thank you post. I posted the code I wrote that got me what I wanted that Dan had sparked. Apparently we were working it at the same time and came up with slightly different methods.
    – Mr Brimm
    Commented Apr 22, 2016 at 1:07

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.