1

I did create some custom endpoints for a plugin i'm working on, with the REST api. It works quite well, but now I would like to secure those requests : I don't want external users (EDIT: I mean remote requests) to be able to do query them.

But I only find documentation about javascript authentification (REST API Handbook). How should I achieve authentification with PHP (WP 5.1.1 here) ?

Thanks

1 Answer 1

-1

What do you mean by "external users"? If you only want Users, that are logged into your website, to use the API Endpoint, you can filter the "rest_authentication_errors" like described here

Add this to your plugin:

add_filter( 'rest_authentication_errors', function( $result ) {
    if ( ! empty( $result ) ) {
        return $result;
    }
    if ( ! is_user_logged_in() ) {
        return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) );
    }
    return $result;
});

Happy Coding!

2
  • Yes, almost - and what if I would like to give some external users access to it ? With a client ID + secret or something ? Thanks.
    – gordie
    Commented Mar 21, 2019 at 8:14
  • Then you don't use this code but instead one of the many Authentification Methods like oAuth, Application Passwords or Basic Authentification (more here: developer.wordpress.org/rest-api/using-the-rest-api/…) and use checks for your REST-API-Endpoint with current_user_can ^^ Commented Mar 21, 2019 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.