• Resolved thitinan

    (@thitinan)


    I used this function in the woocommerce page, my-account/store page.

    But at the my-account page it works fine, while return false in the store page.
    Now I’m temporally using get_current_user_id() instead, but I think it’s may not be the right way.

    Any suggestion ?

    Wordpress v.5.2.3 (with v5.3 is this fixed ?)

    Thank you in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The is_user_logged_in() function can behave unexpectedly is you use it before the template_redirect hook is reached. If you’re using it earlier than that then you can get results that don’t seem correct or it can even cause a fatal error. You will need to make you’re using it at the right time in order for it to be reliable.

    Moderator bcworkz

    (@bcworkz)

    Both get_current_user_id() and is_user_logged_in() rely upon get_current_user(), so there shouldn’t be much difference in results. If you are calling these on template code, it’s certainly after “template_redirect”. But if you are calling within a hook callback, it depends on the hook and Hugh’s comments will apply.

    I believe some API calls will result in an undefined user or if API authentication is through something like oAuth, perhaps a race condition could develop. Both WP and WC rely upon API calls in the back end, so depending on what you are doing, you may be running into API related issues.

    Thread Starter thitinan

    (@thitinan)

    I created the shortcode within the theme function.php (won’t change the theme anyway, self created theme).

    The shortcode is called in the page/some part of template which cannot be directly place, or has some place which can affect multiple places at once.
    The problem is the last one, if it appears in the theme. The get_current_user_id works fine, but is_user_logged_in doesn’t.

    Thread Starter thitinan

    (@thitinan)

    it appears that even the get_current_user_id, cannot be used from time to time.

    Found out it’s the WP Fastest Cache’s problem, can be solved by adding the rules for shop page, and product page.

    • This reply was modified 5 years, 3 months ago by thitinan.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘is_user_logged_in() sometimes not working’ is closed to new replies.