authentication - PHP: authenticate for a REST request?

I did create some custom endpoints for a plugin i'm working on, with the REST api.It works quite well, but now I w

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

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

Share Improve this question edited Mar 21, 2019 at 8:16 gordie asked Mar 21, 2019 at 7:17 gordiegordie 4925 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745669439a4639317.html

相关推荐

  • authentication - PHP: authenticate for a REST request?

    I did create some custom endpoints for a plugin i'm working on, with the REST api.It works quite well, but now I w

    21天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信