4
votes

In a WordPress project, I am using login in frontend. I have included plugin Gravity form. One of the page(e.g. abc) has form with form setting as 'Require user to be logged in'.

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). Why is this happening?

I am using if($("body").hasClass("logged-in")) to check whether user is logged in or not.

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. It comes after refresh. I am confused regarding this cookie.

2
I think you'll need to debug this yourself. I assume you're leaving and returning to your page with the form, i.e. you're not using an AJAX login? When you get back to that page, does the 'logged-in' class exist on the body? Does your jQuery run once the DOM is loaded, and after anything might change on the page? If you delay it by a second or two does it work then? - Rup
@Rup I am using custom login in frontend with registration of users from backend.For login I am using Ajax. When logged in I am always redirecting to a specific login page. For all other pages with no gravity form 'logged-in' class exists on the body but only on menu-child page with gravity form 'logged-in' class appears after refresh. - samjhana joshi

2 Answers

2
votes

You can use Wordpress Default Function for your Project:

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

Add this in JS file that is rendering in the form page ( Check user login like below )

<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.

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');