简体   繁体   English

虽然用户已登录wordpress,但要求用户登录才能使用重力表格

[英]Require user to be logged in not working for gravity form though user is logged in wordpress

In a WordPress project, I am using login in frontend. 在WordPress项目中,我在前端使用登录。 I have included plugin Gravity form. 我已经包含了插件重力形式。 One of the page(eg abc) has form with form setting as 'Require user to be logged in'. 其中一个页面(例如abc)的表单设置为“要求用户登录”。

Issue: When I first log in and go to page abc, form shows(as user is logged in) but when I first go to page abc and then do login although user is logged in form isn't showing(it shows only if I refresh the page). 问题:当我第一次登录并转到页面abc时,表单显示(当用户登录时)但是当我第一次去页面abc然后登录虽然用户登录表单没有显示(仅在我显示时显示)刷新页面)。 Why is this happening? 为什么会这样?

I am using if($("body").hasClass("logged-in")) to check whether user is logged in or not. 我正在使用if($("body").hasClass("logged-in"))来检查用户是否登录。

Any help/suggestions are welcome. 欢迎任何帮助/建议。 One thing I have noticed during 'inspect' in application is a specific cookie wordpress_456b46f4d1347f23495548c111992d89 with path as /wp-content/plugins isn't initially loaded in page with gravity form after login. 我在应用程序中的'inspect'期间注意到的一件事是一个特定的cookie wordpress_456b46f4d1347f23495548c111992d89 ,路径为/wp-content/plugins最初在登录后没有加载重力形式的页面。 It comes after refresh. 刷新之后来了。 I am confused regarding this cookie. 我很困惑这个cookie。

You can use Wordpress Default Function for your Project: 您可以为项目使用Wordpress默认功能:

<?php if(!is_user_logged_in()) { ?>
// code Require user to be logged in
<?php } else { ?>
// Your code
<?php } ?>

Add this in JS file that is rendering in the form page ( Check user login like below ) 将其添加到在表单页面中呈现的JS文件中(如下所示检查用户登录)

<script>
var data = {  action: 'is_user_logged_in' };

jQuery.post(ajaxurl, data, function(response) {
    if(response == 'yes') {
        // user is logged in,
    } else {
        // user is not logged in,
    }
});
</script>

Add this in functions.php of active theme. 在活动主题的functions.php中添加它。

function check_user_logged_in() {
    echo is_user_logged_in()?'yes':'no';
    die();
}
add_action('wp_ajax_nopriv_is_user_logged_in', 'check_user_logged_in');

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM