WP-API / Basic-Auth

Basic Authentication handler for the JSON API, used for development and debugging purposes
761 stars 281 forks source link

register_rest_route (Custom Route) #47

Closed yogevlahyani closed 7 years ago

yogevlahyani commented 7 years ago

Hello, I've been trying to make a basic auth for a custom route that I made but couldn't find how to do so.

Here's my code:

function AllPostsByDESCDate( $data ) { $posts = get_posts(array( 'posts_per_page' => 10, 'paged' => $data['paged'], 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => array( 'post', 'page', 'custom-post-type' ), // custom post types 'post_status' => 'publish', 'suppress_filters' => true ));

if ( empty( $posts ) ) { return null; }

return $posts; }

I can send a get http request without Basic Auth while sending an http request to wp-prefix/v2/posts requires me to Basic Auth.

register_rest_route( 'wp-prefix/v2', '/AllPostsByDESCDate/(?P[a-zA-Z0-9-]+)', array( 'methods' => 'GET', 'callback' => 'AllPostsByDESCDate' ) );

I'm using REST API TOOLBOX (https://he.wordpress.org/plugins/rest-api-toolbox/) to choose which endpoint requires an auth