I'm building a Gatsby site with GraphQL, using WordPress as a headless CMS. I really didn't like the idea of someone landing by chance on the WordPress backend and figuring out how to hack their way in, so I followed @Marcin instructions (see above), i.e. I created
- a new theme folder (I called it turn-off-frontend as per @Marcin) and inside it I placed a
- style.css (using @Marcin default code) and an
- index.php
Inside index.php I followed @dev_masta suggestions, but modified them slightly:
- I didn't put the code inside header.php - I didn't create this file - but put it in index.php.
- I didn't want the redirect to go to the WordPress /wp-login.php. Rather I wanted it to go to my Gatsby site:
Obviously, this is the Gatsby dev environment and will have to be changed to the correct URL when the site goes live.
//index.php
<?php
/* Redirect browser */
header("Location: http://localhost:8000");
exit;
I uploaded the turn-off-frontend theme to wp-content/themes and activated it and it worked. When I went to my WordPress url I was bounced directly to my Gatsby site. However, when I went back inside WordPress admin > Appearance to edit the menu, the 'Menus' link had disappeared!
To fix this, I created a functions.php file inside the turn-off-frontend theme and added the following code (from the Wordpress Codex):
//functions.php
<?php
function register_my_menu() {
register_nav_menu('header-menu',__( 'Header Menu' ));
}
add_action( 'init', 'register_my_menu' );
And the menu was available for adding pages and posts once more.
Hope someone finds this useful.