Vannkorn

Full Stack Web Developer

Close

Make your site private using built-in WordPress login form

You do not need to touch the .htaccess file if you want to protect your entire website with a login screen.

Ads: Register now via this link to receive $100 credit from Vultr

This is helpful when you need a staging website for your client to review before going live.

I previously wrote a blog about creating a password-protected directory by adding some configurations to your Apache2 server on .htaccess file.

This time you do not need to touch the .htaccess file if you want to protect your entire website with a login screen, built-in from WordPress.

I’ve added the following PHP functions to my functions.php file and it works like a charm.

<?php
/**
* Make this site private
*/function make_this_site_private() {
     if ( ! is_user_logged_in() && $GLOBALS['pagenow'] !== 'wp-login.php' ) {
          wp_redirect( wp_login_url( $wp -> request ) );
          exit;
     }
}
add_action( 'wp', 'make_this_site_private' );

/**
* After succesful login,
* redirect to homepage
*/function admin_default_page() {
     return '/';
}
add_filter( 'login_redirect', 'admin_default_page' );

Leave a Reply

Your email address will not be published. Required fields are marked *