The following tutorial column of WordPress will introduce to you how to log in to view the website in WordPress. I hope it will be helpful to friends in need!
If the website content only needs to be viewed by registered users and is hidden from everyone else, and the browser will jump directly to the login registration page without logging in, you can use the following code to achieve this.
Add the following code to the current theme function template functions.php:
Code 1. Access the website without logging in and jump directly to the default login page.
add_action( 'wp', 'login_access' ); function login_access() { global $pagenow; if( !is_user_logged_in() && $pagenow != 'wp-login.php' ) auth_redirect(); }
Code 2 is not logged in to access the website, you can jump to a customized page with 302
add_action( 'template_redirect', 'zm_force_login' ); function zm_force_login() { // 判断登录 if ( ! is_user_logged_in() ) { // 判断HTTPS $schema = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https://' : 'http://'; // 判断站内链接 $url = $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // 添加白名单 $allowed = apply_filters_deprecated( 'zm_force_login_whitelist', array( array( '允许访问的链接1', '允许访问链接2' ) ), '1.0', 'zm_force_login_bypass' ); $bypass = apply_filters( 'zm_force_login_bypass', in_array( $url, $allowed ), $url ); if ( preg_replace( '/\?.*/', '', $url ) !== preg_replace( '/\?.*/', '', wp_login_url() ) && ! $bypass ) { // 防止缓存 nocache_headers(); // 跳转的页面链接 $page = '跳转到的页面链接'; // 执行302跳转 wp_safe_redirect( $page, 302 ); // 跳转到默认登录页面 // wp_safe_redirect( wp_login_url(), 302 ); exit; } } }
Note: The jump page link must be added to the whitelist at the same time, otherwise an infinite loop will be formed.
The above is the detailed content of Implement WordPress login to view the website. For more information, please follow other related articles on the PHP Chinese website!