简体   繁体   English

仅当用户登录时才加载页面

[英]Load Page Only if User Logged in

I'd like to load different pages on a button click on the condition that the user is already logged in (or not). 我想在用户已经登录(或未登录)的条件下,单击按钮加载不同的页面。 I've tried the below code however I'm getting the error "Uncaught ReferenceError: is_user_logged_in is not defined" 我尝试了下面的代码,但是出现错误“ Uncaught ReferenceError:is_user_logged_in未定义”

My current code is: 我当前的代码是:

if(is_user_logged_in()) 
        {
          localStorage.setItem('mood-board', JSON.stringify(selectedOptions));
          window.location.href = "https://manilva.co/click-to-reveal/";
        } 

        else 
        {
          localStorage.setItem('mood-board', JSON.stringify(selectedOptions));
          window.location.href = "https://manilva.co/register/";
        }

The function is_user_logged_in is actually a wordpress internal function and written in PHP . 函数is_user_logged_in实际上是一个wordpress内部函数,用PHP So it can not be used in JS directly so here is the workaround for this. 因此,不能直接在JS使用它,因此这里是解决方法。
So the code should be the following: 因此,代码应如下所示:

<?php if(is_user_logged_in()) { ?>
    <script>
        localStorage.setItem('mood-board', JSON.stringify(selectedOptions));
        window.location.href = "https://manilva.co/click-to-reveal/";
    </script>
<?php } else  { ?>
    <script>
        localStorage.setItem('mood-board', JSON.stringify(selectedOptions));
        window.location.href = "https://manilva.co/register/";
    </script>
<?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果用户登录并具有特定 UID,则仅加载页面的功能? - Function to only load a page if the user is logged in and has a certain UID? 仅当用户登录时在页面加载时发送 GET 请求 - Send a GET request on page load only if user is logged in 如果用户未使用Jquery AJAX登录,如何加载内部日志记录页面 - How load an internal logging page if user not logged in Using Jquery AJAX 在页面加载之前获取登录用户 Javascript Firebase - Get logged in user before page load Javascript Firebase 登录时仅在用户个人资料页面上启用Jeditable - Jeditable enabled only on user profile page when logged in 如果用户未登录,则重定向页面 - Redirecting a page if user not logged in 弹出页面加载仅每个用户加载一次 - Pop up on page load only to load once per user 在Meteor中,如何仅在用户使用特定外部服务登录时才加载JavaScript? - In Meteor, how to only load a javascript when the user is logged in using an particular external service? 仅当用户登录/ localStorage存在时,才如何授予对Vue.js中页面的访问权限? - How to give access to page in Vue.js only if a user is logged in / localStorage exists? 如果用户不滚动自己,如何在加载时滚动页面? - How to scroll the page on load only if the user does not scroll himself?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM