Require login to view WordPress site
Posted in Web Development on 06.10.2015 by @chasebadkids
Posted in Web Development on 06.10.2015 by @chasebadkids
You may want to force users to login in order to view a page within your WordPress site.
You can use the following code to do that:
add_action('template_redirect','giga_check_if_logged_in'); function giga_check_if_logged_in() { if(!is_user_logged_in()) //Are they logged in? If not: { // Get the requested URL global $wp; $requested_url = home_url( $wp->request ); //Set $url to {site_url()}/wp-login.php?redirect_to={$requested_url} $url = add_query_arg( 'redirect_to', $requested_url, site_url('wp-login.php') ); //redirect any request to {site_url()}/wp-login.php?redirect_to={$requested_url} wp_redirect($url); exit; } }