Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Last active January 9, 2024 13:30
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jasonbahl/5dd6c046cd5a5d39bda9eaaf7e32a09d to your computer and use it in GitHub Desktop.
Save jasonbahl/5dd6c046cd5a5d39bda9eaaf7e32a09d to your computer and use it in GitHub Desktop.
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

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