Extending Endpoints | How To | WP OAuth Server

Extending Endpoints

Documentation under "How To" for WP OAuth Server


At some point you may need to change the data provided by WP OAuth Server’s resource endpoints. Data can be modified by using the “wo_endpoints” filter. The following code can be placed in your theme’s functions file or a custom plugin to modify the user endpoint (oauth/me).

<?php
add_filter('wo_endpoints','wo_extend_resource_api', 2);
function wo_extend_resource_api ($methods){
 $methods['me'] = array('func'=>'_wo_me_1');
 return $methods;
}

/**
* Replaces the default me enpoint
* @param [type] $token [description]
* @return [type] [description]
*/
function _wo_me_1 ( $token=null ){
 $user_id = &$token['user_id'];

 global $wpdb;
 $me_data = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}users WHERE ID=$user_id", ARRAY_A);

 /** prevent sensative data - makes me happy ;) */
 unset( $me_data['user_pass'] );
 unset( $me_data['user_activation_key'] );
 unset( $me_data['user_url'] );
 unset( $me_data['session_tokens'] );

 // add user metadata
 $infometa = $wpdb->get_results("SELECT meta_key, meta_value FROM {$wpdb->prefix}usermeta WHERE user_id = ".$user_id."");
 foreach ($infometa as $metarow) {

 $key = $metarow->meta_key;
 if( is_serialized( $metarow->meta_value ) ){
 $me_data[$key] = unserialize( $metarow->meta_value );
 }else{
 $me_data[$key] = $metarow->meta_value;
 }
 }

 $response = new WPOAuth2\Response($me_data);
 $response->send();
 exit;
}