drs7798 2016-10-29 14:24
浏览 68

如何将整个WordPress网站限制为访问者,但注册和登录页面?

Restrict Website, But Login and Register Page


I want my my entire WordPress site to be restricted to visitors, but I want the register and login page to be accessible (not restricted) to the visitors


Since my website is a members-only website, I want to restrict it's access so that visitors only see 2 pages, the login page and the register page.

I have tried multiple plugins to do exactly that but unfortunately for me, I haven't been able to find the right one. Some plugins redirect the entire website to one page while other plugins require custom redirection settings for each page to be separately added.

What I want:-

  1. Restrict entire WordPress website to visitors by redirection.
  2. Do not restrict 2 pages, Login Page and Registration Page.
  3. Redirect Users to Login Page and/or Register Page.

There is an option in my Login Page to view the Register Page.
I want both of these pages to be accessible to the visitor and the rest of the website to be inaccessible.

  • 写回答

1条回答 默认 最新

  • doudui1850 2016-10-29 19:13
    关注

    You can simply redirect your users using template_redirect hook

    add_action( 'template_redirect', 'redirect_users' );
    function redirect_users( $template ) {
        if ( !is_user_logged_in() ) {
            if ( !is_page( 'register page ID' ) ) {
                wp_redirect( wp_login_url() );
            }
        }
    
        return $template;
    }
    

    You need to add register and login page IDs and login page URL in the function.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程