<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rest api Archives - CNERIS</title>
	<atom:link href="https://cneris.com/en/tag/rest-api-en/feed/" rel="self" type="application/rss+xml" />
	<link>https://cneris.com/en/tag/rest-api-en/</link>
	<description></description>
	<lastBuildDate>Tue, 03 Dec 2024 20:52:48 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
	<item>
		<title>How to disable REST API access for unauthenticated users?</title>
		<link>https://cneris.com/en/how-to-disable-rest-api-access-for-unauthenticated-users/</link>
					<comments>https://cneris.com/en/how-to-disable-rest-api-access-for-unauthenticated-users/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 03 Dec 2024 20:52:48 +0000</pubDate>
				<category><![CDATA[Computer Security]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[disable rest api]]></category>
		<category><![CDATA[rest api]]></category>
		<guid isPermaLink="false">https://cneris.com/?p=2728</guid>

					<description><![CDATA[<p>The WordPress REST API is a powerful tool that allows interaction with your site through HTTP requests. However, there may be instances where you want to restrict API access, especially for unauthenticated users, for security or privacy reasons. How to disable REST API access for unauthenticated users? By default, WordPress allows certain REST API endpoints [...]</p>
<p>The post <a href="https://cneris.com/en/how-to-disable-rest-api-access-for-unauthenticated-users/">How to disable REST API access for unauthenticated users?</a> appeared first on <a href="https://cneris.com/en">CNERIS</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The WordPress REST API is a powerful tool that allows interaction with your site through HTTP requests. However, there may be instances where you want to restrict API access, especially for unauthenticated users, for security or privacy reasons.</p>
<h4>How to disable REST API access for unauthenticated users?</h4>
<p>By default, WordPress allows certain REST API endpoints to be publicly accessible. To restrict this access, you can use the <code>rest_authentication_errors</code> filter. The following code blocks REST API requests for any unauthenticated user and returns a custom error message.</p>
<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950">
<blockquote>
<div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none">add_filter(&#8216;rest_authentication_errors&#8217;, function($result) {<br />
if (!empty($result)) {<br />
return $result;<br />
}<br />
if (!is_user_logged_in()) {<br />
return new WP_Error(&#8216;rest_not_logged_in&#8217;, &#8216;You must log in to access the REST API.&#8217;, array(&#8216;status&#8217; =&gt; 401));<br />
}<br />
return $result;<br />
});</div>
</blockquote>
</div>
<h4>What does this code do?</h4>
<ol>
<li><strong>Filter <code>rest_authentication_errors</code></strong>: This filter is used to handle authentication errors in the REST API.</li>
<li><strong>Check for pre-existing errors</strong>: If <code>$result</code> already contains an error, it returns it as is.</li>
<li><strong>User session verification</strong>: If the user is not logged in (<code>!is_user_logged_in()</code>), it returns a <code>WP_Error</code> with a custom message and an HTTP 401 (Unauthorized) status code.</li>
<li><strong>Return results</strong>: If none of the above conditions are met, it proceeds with normal request handling.</li>
</ol>
<h4>Result</h4>
<p>With this code in place, any unauthenticated user will receive an error message stating they must log in to access the REST API. This effectively secures your REST API from unauthorized access.</p>
<p>The post <a href="https://cneris.com/en/how-to-disable-rest-api-access-for-unauthenticated-users/">How to disable REST API access for unauthenticated users?</a> appeared first on <a href="https://cneris.com/en">CNERIS</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://cneris.com/en/how-to-disable-rest-api-access-for-unauthenticated-users/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
