Skip to content

Instantly share code, notes, and snippets.

@vanaf1979
Last active April 15, 2024 10:38
Show Gist options
  • Save vanaf1979/97e9dd42950d82e57717845a4e8d5487 to your computer and use it in GitHub Desktop.
Save vanaf1979/97e9dd42950d82e57717845a4e8d5487 to your computer and use it in GitHub Desktop.
How to create a custom WordPress Rest Api route. https://since1979.dev/snippet-005-simple-custom-rest-api-route/
<?php
/**
* mytheme_get_meta_value.
*
* Register a custom Wp Rest Api end-point,
*
* @see https://since1979.dev/snippet-005-simple-custom-rest-api-route/
*
* @uses register_rest_route() https://developer.wordpress.org/reference/functions/register_rest_route/
* @uses array() https://www.php.net/manual/en/function.array.php
*/
function mytheme_get_meta_value()
{
register_rest_route('mytheme/v1', '/meta/(?P<post>[\d]+)/(?P<key>[a-zA-Z0-9_-]+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'mytheme_handle_get_meta_value',
));
}
/**
* Hook: rest_api_init.
*
* @uses add_action() https://developer.wordpress.org/reference/functions/add_action/
* @uses rest_api_init https://developer.wordpress.org/reference/hooks/rest_api_init/
*/
add_action('rest_api_init', 'mytheme_get_meta_value');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment