Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Disable frontend but allow REST, CRON and GraphQL Requests
<?php
add_action( 'parse_request', 'disable_front_end', 99 );
function disable_front_end() {
global $wp;
/**
* If the request is not part of a CRON, REST Request, GraphQL Request or Admin request,
* output some basic, blank markup
*/
if (
! defined( 'DOING_CRON' ) &&
! defined( 'REST_REQUEST' ) &&
! is_admin() &&
(
empty( $wp->query_vars['rest_oauth1'] ) &&
! defined( 'GRAPHQL_HTTP_REQUEST' )
)
) {
echo '<!doctype html><html><body>&nbsp;</body></html>';
exit;
}
}
@BenMcD0nald
Copy link

BenMcD0nald commented May 26, 2021

Thanks for this

For anyone looking to redirect instead of echo a blank document try:

header('Location:https://<domain-name>/' . $wp->request, true, 301 ); exit();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment