Skip to content

Instantly share code, notes, and snippets.

@onigetoc
Forked from scottopolis/wp-api-user-meta.php
Last active April 24, 2016 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onigetoc/d826d9dece01bd0662e2a240f6b92b05 to your computer and use it in GitHub Desktop.
Save onigetoc/d826d9dece01bd0662e2a240f6b92b05 to your computer and use it in GitHub Desktop.
Add user meta to the WP-API
<?php
/* Adds all user meta to the /wp-json/wp/v2/user/[id] endpoint */
function sb_user_meta( $data, $field_name, $request ) {
if( $data['id'] ){
$user_meta = get_user_meta( $data['id'] );
}
if ( !$user_meta ) {
return new WP_Error( 'No user meta found', 'No user meta found', array( 'status' => 404 ) );
}
foreach ($user_meta as $key => $value) {
$data[$key] = $value;
}
return $data;
}
add_action( 'rest_api_init', function () {
register_api_field( 'user',
'meta',
array(
'get_callback' => 'sb_user_meta',
'update_callback' => null, // add callback here for POST/PUT requests to update user meta
'schema' => null,
)
);
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment