Skip to content

Commit 777b144

Browse files
committed
Adjust formatting of submitted code example
1 parent 8fbc2f0 commit 777b144

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,37 @@ This page provides solutions to some common questions and problems that may aris
88
You should not disable the REST API, because doing so would break future WordPress Admin functionality that will depend 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

1313
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.
1414

15-
Note: The incoming callback parameter can be either null, WP_Error or a boolean. The type of the parameter indicates the state of authentication:
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:
1616

17-
* null: indicates another authentication method should check instead (implies no authentication was performed before)
18-
* boolean: indicates a previous authentication method check was performed. Boolean true would indicate successfully and the user is authenticated. Boolean false not.
19-
* WP_Error: Some kind of error was encountered.
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.
2020

2121
```php
2222
add_filter( 'rest_authentication_errors', function( $result ) {
23-
24-
// A previous authentication check has been performed.
25-
// No need for us to do it again. Return incoming parameter and be done with it.
26-
if ( true === $result || is_wp_error( $result ) ) {
23+
// If a previous authentication check was applied,
24+
// pass that result along without modification.
25+
if ( true === $result || is_wp_error( $result ) ) {
2726
return $result;
28-
}
29-
30-
// No authentication performed yet.
31-
// Check if the user is NOT logged in and return an error
32-
if ( ! is_user_logged_in() ) {
33-
return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) );
34-
}
35-
36-
return $result;
37-
27+
}
28+
29+
// No authentication has been performed yet.
30+
// Return an error if user is not logged in.
31+
if ( ! is_user_logged_in() ) {
32+
return new WP_Error(
33+
'rest_not_logged_in',
34+
__( 'You are not currently logged in.' ),
35+
array( 'status' => 401 )
36+
);
37+
}
38+
39+
// Our custom authentication check should have no effect
40+
// on logged-in requests
41+
return $result;
3842
});
3943
```
4044

0 commit comments

Comments
 (0)