0

Here's my issue:

When a user logs in to our WP site, we want to show the user's first and last name next to the login icon on every page (to show that they're logged in and add some personalization to the page). The problem is, we are using Varnish to cache our pages, so if someone named "Bob C." logs in, it will show that name for everyone.

The way the application works is:
1st hit - browser request-->nginx-->varnish -->apache (php code)
2nd hit - browser request --> nginx-->varnish

So after the first time someone gets to our page, it no longer looks at the php code.
This is where my problem comes in, cause I haven't been able to find a way to gather the user's info without the use of PHP.

What I've tried:

In enqueue-scripts.php

function getUserInfo() {
  global $current_user;
  $current_user = wp_get_current_user();

  wp_enqueue_script( 'getUserInfo', get_template_directory_uri() . {path to isLoggedIn.js}, array( 'jquery' ), '', true );
  wp_localize_script( 'getUserInfo', 'theUser', array(
        'username' => $current_user->user_login,
        'firstname' => $current_user->first_name,
        'lastname' => $current_user->last_name
  ) );
}
add_action( 'wp_enqueue_scripts', 'getUserInfo' );

In isLoggedin.js

if (theUser.firstname) {
     $('#loginDisplayName').text(theUser.firstname + ' ' + theUser.lastname[0] + '.');
   } else {
     $('#loginDisplayName').text('Account');
   }

But since it's using PHP to get the user's info, it doesn't change once cached since Varnish ignores PHP after the first hit.

I know this is a complicated issue, so thank you in advance for anyone willing to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.