Skip to content

Instantly share code, notes, and snippets.

@scottopolis
Last active April 18, 2020 19:13
Show Gist options
  • Select an option

  • Save scottopolis/52d00e2095d1e959d2ef to your computer and use it in GitHub Desktop.

Select an option

Save scottopolis/52d00e2095d1e959d2ef to your computer and use it in GitHub Desktop.

Revisions

  1. scottopolis revised this gist Oct 29, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions wp-api-user-meta.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?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'] ){
  2. scottopolis created this gist Oct 29, 2015.
    27 changes: 27 additions & 0 deletions wp-api-user-meta.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    /* 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,
    )
    );
    } );