Created
December 29, 2015 22:29
-
-
Save JordanForeman/b2566c4537d5c70e75da to your computer and use it in GitHub Desktop.
Extending WP API - Code Sample 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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