{"id":163734,"date":"2022-10-20T00:49:55","date_gmt":"2022-10-20T00:49:55","guid":{"rendered":"https:\/\/wordpress.org\/plugins\/rest-api-guard\/"},"modified":"2025-09-09T20:00:57","modified_gmt":"2025-09-09T20:00:57","slug":"rest-api-guard","status":"publish","type":"plugin","link":"https:\/\/hu.wordpress.org\/plugins\/rest-api-guard\/","author":5853094,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_crdt_document":"","version":"1.4.1","stable_tag":"1.4.1","tested":"6.8.5","requires":"6.5","requires_php":"8.1","requires_plugins":null,"header_name":"REST API Guard","header_author":"Sean Fisher","header_description":"Restrict and control access to the REST API","assets_banners_color":"","last_updated":"2025-09-09 20:00:57","external_support_url":"","external_repository_url":"","donate_link":"","header_plugin_uri":"https:\/\/github.com\/alleyinteractive\/wp-rest-api-guard","header_author_uri":"https:\/\/alley.com\/","rating":5,"author_block_rating":0,"active_installs":100,"downloads":3653,"num_ratings":2,"support_threads":0,"support_threads_resolved":0,"author_block_count":0,"sections":["installation","description"],"tags":{"1.0.0":{"tag":"1.0.0","author":"sean212","date":"2022-10-20 00:56:42"},"1.0.1":{"tag":"1.0.1","author":"sean212","date":"2022-10-26 15:34:21"},"1.0.2":{"tag":"1.0.2","author":"sean212","date":"2022-11-03 19:26:19"},"1.0.3":{"tag":"1.0.3","author":"sean212","date":"2023-08-28 20:31:01"},"1.1.2":{"tag":"1.1.2","author":"sean212","date":"2024-01-15 15:49:00"},"1.2.0":{"tag":"1.2.0","author":"sean212","date":"2024-02-23 14:39:13"},"1.2.1":{"tag":"1.2.1","author":"sean212","date":"2024-02-28 19:25:17"},"1.3.0":{"tag":"1.3.0","author":"sean212","date":"2024-02-28 19:26:48"},"1.3.1":{"tag":"1.3.1","author":"sean212","date":"2024-03-20 15:15:21"},"1.3.2":{"tag":"1.3.2","author":"sean212","date":"2024-03-20 15:21:17"},"1.4.1":{"tag":"1.4.1","author":"sean212","date":"2025-09-09 20:00:57"}},"upgrade_notice":[],"ratings":{"1":0,"2":0,"3":0,"4":0,"5":2},"assets_icons":{"icon-128x128.png":{"filename":"icon-128x128.png","revision":3111870,"resolution":"128x128","location":"assets","locale":""},"icon-256x256.png":{"filename":"icon-256x256.png","revision":3111870,"resolution":"256x256","location":"assets","locale":""}},"assets_banners":[],"assets_blueprints":{},"all_blocks":[],"tagged_versions":["1.0.0","1.0.1","1.0.2","1.0.3","1.1.2","1.2.0","1.2.1","1.3.0","1.3.1","1.3.2","1.4.1"],"block_files":[],"assets_screenshots":[],"screenshots":[],"jetpack_post_was_ever_published":false},"plugin_section":[],"plugin_tags":[214748,214749],"plugin_category":[],"plugin_contributors":[78228],"plugin_business_model":[],"class_list":["post-163734","plugin","type-plugin","status-publish","hentry","plugin_tags-alleyinteractive","plugin_tags-rest-api-guard","plugin_contributors-sean212","plugin_committers-sean212"],"banners":[],"icons":{"svg":false,"icon":"https:\/\/ps.w.org\/rest-api-guard\/assets\/icon-128x128.png?rev=3111870","icon_2x":"https:\/\/ps.w.org\/rest-api-guard\/assets\/icon-256x256.png?rev=3111870","generated":false},"screenshots":[],"raw_content":"<!--section=installation-->\n<p>You can install the package via composer:<\/p>\n\n<pre><code>composer require alleyinteractive\/wp-rest-api-guard\n<\/code><\/pre>\n\n<!--section=description-->\n<p>Restrict and control access to the REST API.<\/p>\n\n<h3>Usage<\/h3>\n\n<p>The WordPress REST API is generally very public and can share a good deal of information with the internet anonymously. This plugin aims to make it easier to restrict access to the REST API for your WordPress site.<\/p>\n\n<p>Out of the box the plugin can:<\/p>\n\n<ul>\n<li>Disable anonymous access to the REST API.<\/li>\n<li>Restrict and control anonymous access to the REST API by namespace, path, etc.<\/li>\n<\/ul>\n\n<h3>Settings Page<\/h3>\n\n<p>The plugin can be configured via the Settings page (<code>Settings -&gt; REST API Guard<\/code>) or via the relevant filter.<\/p>\n\n<p><\/p>\n\n<h3>Preventing Access to User Information (<code>wp\/v2\/users<\/code>)<\/h3>\n\n<p>By default, the plugin will restrict anonymous access to the users endpoint. This can be prevented in the plugin's settings or via code:<\/p>\n\n<pre><code>add_filter( 'rest_api_guard_allow_user_access', fn () =&gt; true );\n<\/code><\/pre>\n\n<h3>Preventing Access to Index (<code>\/<\/code>) or Namespace Endpoints (<code>wp\/v2<\/code>)<\/h3>\n\n<p>To prevent anonymous users from browsing your site and discovering what plugins\/post types are setup, the plugin restricts access to the index (<code>\/<\/code>) and namespace (<code>wp\/v2<\/code>) endpoints. This can be prevented in the plugin's settings or via code:<\/p>\n\n<pre><code>\/\/ Allow index access.\nadd_filter( 'rest_api_guard_allow_index_access', fn () =&gt; true );\n\n\/\/ Allow namespace access.\nadd_filter( 'rest_api_guard_allow_namespace_access', fn ( string $namespace ) =&gt; true );\n<\/code><\/pre>\n\n<h3>Restrict Anonymous Access to the REST API<\/h3>\n\n<p>The plugin can restrict anonymous access for any request to the REST API in the plugin's settings or via code:<\/p>\n\n<pre><code>add_filter( 'rest_api_guard_prevent_anonymous_access', fn () =&gt; true );\n<\/code><\/pre>\n\n<h3>Limit Anonymous Access to Specific Namespaces\/Routes (Allowlist)<\/h3>\n\n<p>Anonymous users can be granted access only to specific namespaces\/routes. Requests outside of these paths will be denied. This can be configured in the plugin's settings or via code:<\/p>\n\n<pre><code>add_filter(\n    'rest_api_guard_anonymous_requests_allowlist',\n    function ( array $paths, WP_REST_Request $request ): array {\n        \/\/ Allow other paths not included here will be denied.\n        $paths[] = 'wp\/v2\/post';\n        $paths[] = 'custom-namespace\/v1\/public\/*';\n\n        return $paths;\n    },\n    10,\n    2\n);\n<\/code><\/pre>\n\n<h3>Restrict Anonymous Access to Specific Namespaces\/Routes (Denylist)<\/h3>\n\n<p>Anonymous users can be restricted from specific namespaces\/routes. This acts as\na denylist for specific paths that an anonymous user cannot access. The paths\nsupport regular expressions for matching. The use of the allowlist takes\npriority over this denylist. This can be configured in the plugin's settings or\nvia code:<\/p>\n\n<pre><code>add_filter(\n    'rest_api_guard_anonymous_requests_denylist',\n    function ( array $paths, WP_REST_Request $request ): array {\n        $paths[] = 'wp\/v2\/user';\n        $paths[] = 'custom-namespace\/v1\/private\/*';\n\n        return $paths;\n    },\n    10,\n    2\n);\n<\/code><\/pre>\n\n<h3>Require JSON Web Token (JWT) Authentication<\/h3>\n\n<p>Anonymous users can be required to authenticate via a JSON Web Token (JWT) to\naccess the REST API. Users should pass an <code>Authorization: Bearer &lt;token&gt;<\/code> header\nwith their request. This can be configured in the plugin's settings or via code:<\/p>\n\n<pre><code>add_filter( 'rest_api_guard_authentication_jwt', fn () =&gt; true );\n<\/code><\/pre>\n\n<p>Out of the box, the plugin will look for a JWT in the <code>Authorization: Bearer\n&lt;token&gt;<\/code> header. The JWT will be expected to have an audience of\n'wordpress-rest-api' and issuer of the site's URL. This can be configured in the\nplugin's settings or via code:<\/p>\n\n<pre><code>add_filter(\n    'rest_api_guard_jwt_audience',\n    function ( string $audience ): string {\n        return 'custom-audience';\n    }\n);\n\nadd_filter(\n    'rest_api_guard_jwt_issuer',\n    function ( string $issuer ): string {\n        return 'https:\/\/example.com';\n    }\n);\n<\/code><\/pre>\n\n<p>The JWT's secret will be autogenerated and stored in the database in the\n    rest_api_guard_jwt_secret option. The secret can also be changed via code:<\/p>\n\n<pre><code>add_filter(\n    'rest_api_guard_jwt_secret',\n    function ( string $secret ): string {\n        return 'my-custom-secret';\n    }\n);\n<\/code><\/pre>\n\n<h3>Allow JWT Authentication for Authenticated Users<\/h3>\n\n<p>Authenticated users can be authenticated with the REST API via a JSON Web Token.\nSimilar to the anonymous JWT authentication, users should pass an\n    Authorization: Bearer  header with their request. This can be\nconfigured in the plugin's settings or via code:<\/p>\n\n<pre><code>add_filter( 'rest_api_guard_user_authentication_jwt', fn () =&gt; true );\n<\/code><\/pre>\n\n<h3>Generating JWTs for Anonymous and Authenticated Users<\/h3>\n\n<p>JWTs can be generated by calling the\n    wp rest-api-guard generate-jwt [--user=] command or using the\n    Alley\\WP\\REST_API_Guard\\generate_jwt() method:<\/p>\n\n<pre><code>$jwt = \\Alley\\WP\\REST_API_Guard\\generate_jwt(\n    expiration: 3600, \/\/ Optional. The expiration time in seconds from now.\n    user: 1, \/\/ Optional. The user ID to generate the JWT for. Supports `WP_User` or user ID.\n);\n<\/code><\/pre>","raw_excerpt":"Restrict and control access to the REST API.","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/hu.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin\/163734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hu.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin"}],"about":[{"href":"https:\/\/hu.wordpress.org\/plugins\/wp-json\/wp\/v2\/types\/plugin"}],"replies":[{"embeddable":true,"href":"https:\/\/hu.wordpress.org\/plugins\/wp-json\/wp\/v2\/comments?post=163734"}],"author":[{"embeddable":true,"href":"https:\/\/hu.wordpress.org\/plugins\/wp-json\/wporg\/v1\/users\/sean212"}],"wp:attachment":[{"href":"https:\/\/hu.wordpress.org\/plugins\/wp-json\/wp\/v2\/media?parent=163734"}],"wp:term":[{"taxonomy":"plugin_section","embeddable":true,"href":"https:\/\/hu.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_section?post=163734"},{"taxonomy":"plugin_tags","embeddable":true,"href":"https:\/\/hu.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_tags?post=163734"},{"taxonomy":"plugin_category","embeddable":true,"href":"https:\/\/hu.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_category?post=163734"},{"taxonomy":"plugin_contributors","embeddable":true,"href":"https:\/\/hu.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_contributors?post=163734"},{"taxonomy":"plugin_business_model","embeddable":true,"href":"https:\/\/hu.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_business_model?post=163734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}