Instantly share code, notes, and snippets.

Embed
What would you like to do?
Allow WordPress REST API Requests While Restricted Site Access is Active
<?php
/**
* Plugin Name: Unrestrict REST API
* Plugin URI: https://philipnewcomer.net/2016/05/allow-rest-api-restricted-site-access/
* Description: Allows REST API requests while Restricted Site Access is enabled.
* Version: 0.1.0
* Author: Philip Newcomer
* Author URI: https://philipnewcomer.net
*/
/**
* Filter Restricted Site Access to allow REST API requests.
*
* @param bool $is_restricted Whether access is restricted.
* @param object $wp The WordPress object.
*
* @return bool Whether access should be restricted.
*/
function pn_unrestrict_rest_api( $is_restricted, $wp ) {
if ( ! empty( $wp->query_vars['rest_route'] ) ) {
return false;
}
return $is_restricted;
}
add_filter( 'restricted_site_access_is_restricted', 'pn_unrestrict_rest_api', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment