Skip to content

Commit 1d3af45

Browse files
committed
Merge branch 'BjornW:master'
2 parents 9a13847 + 777b144 commit 1d3af45

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

using-the-rest-api/frequently-asked-questions.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,36 @@ This page provides solutions to some common questions and problems that may aris
88
You should not disable the REST API, because doing so will break WordPress Admin functionality that depends on the API being active. However, you may use a filter to require that API consumers be authenticated, which effectively prevents anonymous external access. See below for more information.
99

1010

11-
## Require Authentication for All Reque​sts
11+
## Require Authentication for All Requests
1212

13-
You can require authentication for all REST API requests by adding an `is_user_logged_in` check to the [`rest_authentication_errors`](https://developer.wordpress.org/reference/hooks/rest_authentication_errors/) filter:
13+
You can require authentication for all REST API requests by adding an `is_user_logged_in` check to the [`rest_authentication_errors`](https://developer.wordpress.org/reference/hooks/rest_authentication_errors/) filter.
14+
15+
Note: The incoming callback parameter can be either `null`, a `WP_Error`, or a boolean. The type of the parameter indicates the state of authentication:
16+
17+
* `null`: no authentication check has yet been performed, and the hook callback may apply custom authentication logic.
18+
* boolean: indicates a previous authentication method check was performed. Boolean `true` indicates the request was successfully authenticated, and boolean `false` indicates authentication failed.
19+
* `WP_Error`: Some kind of error was encountered.
1420

1521
```php
1622
add_filter( 'rest_authentication_errors', function( $result ) {
17-
if ( ! empty( $result ) ) {
23+
// If a previous authentication check was applied,
24+
// pass that result along without modification.
25+
if ( true === $result || is_wp_error( $result ) ) {
1826
return $result;
1927
}
28+
29+
// No authentication has been performed yet.
30+
// Return an error if user is not logged in.
2031
if ( ! is_user_logged_in() ) {
21-
return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) );
32+
return new WP_Error(
33+
'rest_not_logged_in',
34+
__( 'You are not currently logged in.' ),
35+
array( 'status' => 401 )
36+
);
2237
}
38+
39+
// Our custom authentication check should have no effect
40+
// on logged-in requests
2341
return $result;
2442
});
2543
```

0 commit comments

Comments
 (0)