Created
September 15, 2024 18:44
-
-
Save atomjoy/e68bae350b97b747526cf025ed3720cb to your computer and use it in GitHub Desktop.
Wordpress custom rest api routes.
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
<?php | |
/** | |
* Subscribe user | |
*/ | |
function subscribe_user(WP_REST_Request $request) { | |
// $params = $request->get_params(); | |
// $email = $params['email']; | |
$email = $request->get_param('email'); | |
return [ | |
'email' => $email, | |
'message' => 'Subscribed', | |
]; | |
} | |
// 'wp.xxx/wp-json/wow/v1/[email protected]' | |
add_action( 'rest_api_init', function () { | |
register_rest_route( 'wow/v1', '/subscribe', array( | |
'methods' => 'GET', | |
'callback' => 'subscribe_user', | |
)); | |
}); | |
// 'wp.xxx/wp-json/wow/v1/subscribe/[email protected]' | |
add_action( 'rest_api_init', function () { | |
// register_rest_route( 'wow/v1', '/subscribe/(.+)', array( | |
register_rest_route( 'wow/v1', '/subscribe/(?P<email>[a-z0-9\@\.\-]+)', array( | |
'methods' => 'GET', | |
'callback' => 'subscribe_user', | |
)); | |
}); | |
/** | |
* User map location | |
*/ | |
function wow_map_location(WP_REST_Request $request) { | |
return $request->get_params(); | |
} | |
// 'wp.xxx/wp-json/wow/v1/map/location/(?P<market>[a-zA-Z0-9-]+)/(?P<lat>[a-z0-9 .\-]+)/(?P<long>[a-z0-9 .\-]+)' | |
add_action( 'rest_api_init', function () { | |
register_rest_route( 'wow/v1', '/map/location', array( | |
'methods' => 'GET', | |
'callback' => 'wow_map_location', | |
)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment