Skip to main content
We are Brand SEO Beijing serving international business, your marketing partner, Contact us by mi@mgsh.com.cn

Login to allow viewing of website content

WordPress login to view the website content, jump to the login page if you are not logged in.There are quite a few people who have such needs, some may be data or something, and some are just used for edge balls.
The implementation code is very simple. Just find a hook that will be executed by the whole site. Here we choose a template_redirect hook. This action hook is executed when the template is loaded. Of course, many others can be used.

<?php
//template_redirect动作钩子是一定会执行的,所以用这个钩子对全站有效
add_action( 'template_redirect', 'ashuwp_show_only_login', 0 );
function ashuwp_show_only_login(){
  //判断登录
  if( !is_user_logged_in() ){
    auth_redirect(); //跳转到登录页面
        exit();
  }
}

For more complex requirements, someone needs to leave one or two more pages on the entire site, such as leaving a "announcement description" page for access, then add a page judgment to the code for judging the login, for example:

<?php
//template_redirect动作钩子是一定会执行的,所以用这个钩子对全站有效
add_action( 'template_redirect', 'ashuwp_show_only_login', 0 );
function ashuwp_show_only_login(){
  //判断登录,about页面就允许访问
  if( !is_page('about') && !is_user_logged_in() ){
    auth_redirect(); //跳转到登录页面
        exit();
  }
}

Do you have any other weirder needs?Add the judgment conditions according to the above example.

Back to Top