Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement WordPress login to view the website

2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report