{"id":305,"date":"2024-02-17T20:36:10","date_gmt":"2024-02-17T20:36:10","guid":{"rendered":"https:\/\/www.simons.tools\/en\/?p=305"},"modified":"2024-02-17T20:36:10","modified_gmt":"2024-02-17T20:36:10","slug":"disable-unauthenticated-access-to-wp_json-api","status":"publish","type":"post","link":"https:\/\/www.simons.tools\/en\/disable-unauthenticated-access-to-wp_json-api\/","title":{"rendered":"Disable unauthenticated Access to WP_JSON API"},"content":{"rendered":"\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;file&quot;,&quot;fullScreenButton&quot;:false,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">add_filter( 'rest_authentication_errors', function( $result ) {\n    \/\/ If a previous authentication check was applied,\n    \/\/ pass that result along without modification.\n    if ( true === $result || is_wp_error( $result ) ) {\n        return $result;\n    }\n\n    \/\/ No authentication has been performed yet.\n    \/\/ Return an error if user is not logged in.\n    if ( ! is_user_logged_in() ) {\n        return new WP_Error(\n            'rest_not_logged_in',\n            __( 'You are not currently logged in.' ),\n            array( 'status' =&gt; 401 )\n        );\n    }\n\n    \/\/ Our custom authentication check should have no effect\n    \/\/ on logged-in requests\n    return $result;\n});\n<\/pre><\/div>\n\n\n\n<p><a href=\"https:\/\/developer.wordpress.org\/rest-api\/frequently-asked-questions\/#can-i-disable-the-rest-api\">https:\/\/developer.wordpress.org\/rest-api\/frequently-asked-questions\/#can-i-disable-the-rest-api<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/developer.wordpress.org\/rest-api\/frequently-asked-questions\/#can-i-disable-the-rest-api<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_en_post_content":"<!-- wp:codemirror-blocks\/code-block {\"fileName\":\"PHP\",\"mode\":\"htmlmixed\",\"mime\":\"text\/html\"} -->\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>add_filter( 'rest_authentication_errors', function( $result ) {\n    \/\/ If a previous authentication check was applied,\n    \/\/ pass that result along without modification.\n    if ( true === $result || is_wp_error( $result ) ) {\n        return $result;\n    }\n\n    \/\/ No authentication has been performed yet.\n    \/\/ Return an error if user is not logged in.\n    if ( ! is_user_logged_in() ) {\n        return new WP_Error(\n            'rest_not_logged_in',\n            __( 'You are not currently logged in.' ),\n            array( 'status' =&gt; 401 )\n        );\n    }\n\n    \/\/ Our custom authentication check should have no effect\n    \/\/ on logged-in requests\n    return $result;\n});\n<\/pre><\/div>\n<!-- \/wp:codemirror-blocks\/code-block -->\n\n<!-- wp:paragraph -->\n<p><a href=\"https:\/\/developer.wordpress.org\/rest-api\/frequently-asked-questions\/#can-i-disable-the-rest-api\">https:\/\/developer.wordpress.org\/rest-api\/frequently-asked-questions\/#can-i-disable-the-rest-api<\/a><\/p>\n<!-- \/wp:paragraph -->","_en_post_name":"disable-unauthenticated-access-to-wp_json-api","_en_post_excerpt":"","_en_post_title":"Disable unauthenticated Access to WP_JSON API","_de_post_content":"","_de_post_name":"","_de_post_excerpt":"","_de_post_title":"","edit_language":"en","footnotes":""},"categories":[12],"tags":[],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"_links":{"self":[{"href":"https:\/\/www.simons.tools\/en\/wp-json\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.simons.tools\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.simons.tools\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.simons.tools\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.simons.tools\/en\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":1,"href":"https:\/\/www.simons.tools\/en\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":306,"href":"https:\/\/www.simons.tools\/en\/wp-json\/wp\/v2\/posts\/305\/revisions\/306"}],"wp:attachment":[{"href":"https:\/\/www.simons.tools\/en\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.simons.tools\/en\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.simons.tools\/en\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}