PHP code example of alleyinteractive / wp-rest-api-guard

1. Go to this page and download the library: Download alleyinteractive/wp-rest-api-guard library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alleyinteractive / wp-rest-api-guard example snippets


add_filter( 'rest_api_guard_allow_user_access', fn () => true );

// Allow index access.
add_filter( 'rest_api_guard_allow_index_access', fn () => true );

// Allow namespace access.
add_filter( 'rest_api_guard_allow_namespace_access', fn ( string $namespace ) => true );

add_filter( 'rest_api_guard_prevent_anonymous_access', fn () => true );

add_filter(
	'rest_api_guard_anonymous_requests_allowlist',
	function ( array $paths, WP_REST_Request $request ): array {
		// Allow other paths not 

add_filter(
	'rest_api_guard_anonymous_requests_denylist',
	function ( array $paths, WP_REST_Request $request ): array {
		$paths[] = 'wp/v2/user';
		$paths[] = 'custom-namespace/v1/private/*';

		return $paths;
	},
	10,
	2
);

add_filter( 'rest_api_guard_authentication_jwt', fn () => true );

add_filter( 'rest_api_guard_jwt_audience', fn ( string $audience ) => 'custom-audience' );

add_filter( 'rest_api_guard_jwt_issuer', fn ( string $issuer ) => 'https://example.com' );

add_filter( 'rest_api_guard_jwt_secret', fn ( string $secret ) => 'my-custom-secret' );

add_filter( 'rest_api_guard_user_authentication_jwt', fn () => true );

$jwt = \Alley\WP\REST_API_Guard\generate_jwt(
	expiration: 3600, // Optional. The expiration time in seconds from now.
	user: 1, // Optional. The user ID to generate the JWT for. Supports `WP_User` or user ID.
);