Skip to main content
added 186 characters in body
Source Link
Cedon
  • 1.7k
  • 2
  • 13
  • 26

I got it. Turns out the tutorial I was looking at was old and I was using the wrong WP function. I was using register_api_field but the correct one to use is register_rest_field.

It goes like this...

function facebook_add_user_data() {
register_rest_field( 'user',
    'facebook',
    array(
        'get_callback'  => 'facebook_get_user_field''rest_get_user_field',
        'update_callback'   => null,
        'schema'            => null,
     )
);
}
add_action( 'rest_api_init', 'facebook_add_user_data' );

function facebook_get_user_fieldrest_get_user_field( $user, $field_name, $request ) {
    return get_user_meta( $user[ 'id' ], $field_name, true );
}

I tried it out and the response from the server included the "facebook" field and the URL from the user's profile.

Also, the facebook_get_user_field function can actually be reused so I renamed it rest_get_user_field and tested it with another field name nad it produced that data in the response too.

I got it. Turns out the tutorial I was looking at was old and I was using the wrong WP function. I was using register_api_field but the correct one to use is register_rest_field.

It goes like this...

function facebook_add_user_data() {
register_rest_field( 'user',
    'facebook',
    array(
        'get_callback'  => 'facebook_get_user_field',
        'update_callback'   => null,
        'schema'            => null,
     )
);
}
add_action( 'rest_api_init', 'facebook_add_user_data' );

function facebook_get_user_field( $user, $field_name, $request ) {
    return get_user_meta( $user[ 'id' ], $field_name, true );
}

I tried it out and the response from the server included the "facebook" field and the URL from the user's profile.

I got it. Turns out the tutorial I was looking at was old and I was using the wrong WP function. I was using register_api_field but the correct one to use is register_rest_field.

It goes like this...

function facebook_add_user_data() {
register_rest_field( 'user',
    'facebook',
    array(
        'get_callback'  => 'rest_get_user_field',
        'update_callback'   => null,
        'schema'            => null,
     )
);
}
add_action( 'rest_api_init', 'facebook_add_user_data' );

function rest_get_user_field( $user, $field_name, $request ) {
    return get_user_meta( $user[ 'id' ], $field_name, true );
}

I tried it out and the response from the server included the "facebook" field and the URL from the user's profile.

Also, the facebook_get_user_field function can actually be reused so I renamed it rest_get_user_field and tested it with another field name nad it produced that data in the response too.

Source Link
Cedon
  • 1.7k
  • 2
  • 13
  • 26

I got it. Turns out the tutorial I was looking at was old and I was using the wrong WP function. I was using register_api_field but the correct one to use is register_rest_field.

It goes like this...

function facebook_add_user_data() {
register_rest_field( 'user',
    'facebook',
    array(
        'get_callback'  => 'facebook_get_user_field',
        'update_callback'   => null,
        'schema'            => null,
     )
);
}
add_action( 'rest_api_init', 'facebook_add_user_data' );

function facebook_get_user_field( $user, $field_name, $request ) {
    return get_user_meta( $user[ 'id' ], $field_name, true );
}

I tried it out and the response from the server included the "facebook" field and the URL from the user's profile.