I’m using the WordPress database and back end to administer the news for my band’s website and everything is working great however I’d like to disable the front end of WordPress itself.
I have the WordPress installation installed in /wordpress/
and obviously the admin section is under /wordpress/wp-admin/
.
What would be the best way to restrict someone from accessing the rather *un*setup WordPress site itself without affecting the admin section?
If anything, I could simply redirect to the website’s proper home page (domain.com/
).
To make sure only the front end redirects to
domain.com
, make a theme that uses the PHP header() function.Create a folder called redirect or
something.
Add two files to the
folder:
style.css
andindex.php
(necessary for a valid WP theme)
In
style.css
, add something likethis:
In
index.php
add this:Upload the folder to the themes directory and then activate it in the admin UI.
Use a theme with “empty data”. Put two files in directory, then activate “theme”.
style.css
and index.php
Although this is a rather old question with an already accepted answer, someone might find this useful, specially since none of these solutions worked for me.
The code itself is pretty explanatory:
Just put the code in any plugin or the theme’s function.php and it should work out of the box.
EDIT:
If this is not working for you (I had minor issues even with this code), you can create a new theme (or a child theme) and put only this content inside the
header.php
file:Put this in your .htaccess and list the paths you want to keep available:
IMO, a plugin would require less work and is more appropriate for the specific case.
add this to the .htaccess in your root directory
EDIT: This is really just a quick fix, there might be better solutions. Another way would be to add a function to your functions.php file, that is then called in wp_head() to redirect that way. Using that method you could also allow yourself to view it with a simple IP check.
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
Inside index.php I followed @dev_masta suggestions, but modified them slightly:
Obviously, this is the Gatsby dev environment and will have to be changed to the correct URL when the site goes live.
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):
And the menu was available for adding pages and posts once more.
Hope someone finds this useful.