Skip to content

Instantly share code, notes, and snippets.

@JordanForeman
Created December 29, 2015 22:29
Show Gist options
  • Select an option

  • Save JordanForeman/b2566c4537d5c70e75da to your computer and use it in GitHub Desktop.

Select an option

Save JordanForeman/b2566c4537d5c70e75da to your computer and use it in GitHub Desktop.
Extending WP API - Code Sample 3
namespace HM\REST_API;
class REST_API {
public function __construct() {
add_action('rest_api_init', array($this, 'register_routes'));
}
public function register_routes() {
$api_namespace = 'hm/v1';
// Register our first endpoint
register_rest_route(
$api_namespace,
'/user',
array(
'methods' => 'GET',
'callback' => array($this, 'get_user_callback')
)
);
}
public function get_user_callback( $request ) {
$user_email = $request->get_param('email');
$user = get_user_by( 'email', $user_email );
if ( !$user ) {
return array(
'error' => 'User not found'
);
} else {
return $user;
}
}
}
new REST_API();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment