Skip to content

Instantly share code, notes, and snippets.

@adamrosloniec
Last active August 9, 2019 03:08
Show Gist options
  • Save adamrosloniec/54f2ab0427cef7f86b439dce624d87a0 to your computer and use it in GitHub Desktop.
Save adamrosloniec/54f2ab0427cef7f86b439dce624d87a0 to your computer and use it in GitHub Desktop.
WordPress - Hide Login Page with custom hidden $_GET and show website only via Rest API endpoints
<?php
function hide_login_page_and_show_only_rest_api_endpoints() {
// work only on productions sites
if (
!stristr($_SERVER['SERVER_NAME'], 'local')
&& !is_user_logged_in()
) {
if (
(
in_array( $GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php')
&& isset($_GET['your_custom_get'])
&& $_GET['your_custom_get'] === 'true'
) || (
stristr($_SERVER['REQUEST_URI'], '/wp-json/')
)
) {
// ok
} else {
// die
global $wp_query;
$wp_query->set_404();
status_header( 404 );
nocache_headers();
die();
}
}
}
add_action( 'wp_loaded', 'hide_login_page_and_show_only_rest_api_endpoints' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment