Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to implement WordPress login to view the website

Shulou Source: shulou.com Published: 2022-06-01 23:54:42 10月21日 Update

This article mainly introduces how to achieve WordPress login to view the website, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

If the content of the site only wants to be browsed by registered users and hidden from everyone else, visitors can jump to the login registration page without logging in, which can be realized with the following code.

Add the following code to the current theme function template functions.php:

Code one does not log in to visit the website to 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 does not log in to visit the website, you can jump 302 to a custom page

Add_action ('template_redirect',' zm_force_login'); function zm_force_login () {/ / determine login if (! Is_user_logged_in () {/ / judge HTTPS$schema = isset ($_ SERVER ['HTTPS']) & &' on' = $_ SERVER ['HTTPS']? 'https://':' http://';// determines the link within the site $url = $schema. $_ SERVER ['HTTP_HOST']. $_ SERVER ['REQUEST_URI']; / add whitelist $allowed = apply_filters_deprecated (' zm_force_login_whitelist', array ('array (' allowed links 1', 'allow access 2')), '1.0 links,' 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) {/ / prevent caching nocache_headers (); / / page link to jump $page = 'page link to jump to'; / / perform 302 jump wp_safe_redirect ($page, 302) / / Jump to the default login page / / wp_safe_redirect (wp_login_url (), 302); exit;}}

Note: links to redirected pages should be added to the whitelist at the same time, otherwise it will form an endless loop.

Thank you for reading this article carefully. I hope the article "how to achieve WordPress login and view website" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Login page link website code article at the same time list browse topic value interest content function owner more friends template viewer user Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux OPPO Reno Shulou Technology Huawei Xiaomi