skip to Main Content

I’m currently trying to figure out how to detect if a WordPress user is logged in when browsing the decoupled frontend (NextJS app, GraphQL via the WPGraphQL plugin) for showing options based on this, like an edit button for the current page. It’s a simple check on monolithic WordPress pages, but I’m clueless about doing it this way.

2

Answers


  1. If a user is logged in there should be a key in the local or session storage you can check for. If it it populated that user is logged in. I would reach out to you team to learn what key to look for. Code for doing this would look similar to

    const key = localStorage.getItem("KEY");
    
    if (key != null):
        // user is logged in
    

    https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem

    Login or Signup to reply.
  2. Hacky, but if you have the default wp body class you can check it logged-in. Not tested, but shouuld work.
    get_body_class has :

    if ( is_user_logged_in() ) {
            $classes[] = 'logged-in';
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search