I would like to use the user name of a logged in user in JavaScript without server side implementation.

Since I'm using a cache plugin (WP Super Cache), I don't want to place a user name into the generated HTML output of WordPress. But I would like to use it to fill a contact form, if the user is already logged in.

Is there a way without a separate AJAX request?

  • 1
    Without AJAX? No. May 8, 2020 at 3:39
  • FWIW W3 Total Cache appears to support something like this using special tags in the template: justinsilver.com/technology/wordpress/… May 8, 2020 at 4:07
  • @Jacob Peattie: ... but I can use the wp-settings-... cookie, to get the WordPress user Id. ;-)
    – Tahtu
    May 8, 2020 at 13:51
  • Only if the user has been to /wp-admin, and you still need an AJAX request to get the user name. May 8, 2020 at 14:02
  • Can you clarify why you're trying to avoid a server-side implementation? If it's simply to avoid the caching, but you're otherwise able to make PHP changes, you can pass the username to your Javascript using this technique: blog.wplauncher.com/… May 13, 2020 at 0:58


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.