3

How can I get the logged in username in a jQuery script?

Background:

I'm working with ACF repeater fields, and I want to auto populate a subfield with the logged in username. This should be executed each time the user adds a new row in the repeatable field.

Everything looks pretty well, but I'm stuck at the point of getting the logged in username in jQuery. I don't know how to get it. Any guidance will be appreciated!

  • Are you using this code in a .js file or a .php file ? – Abdul Awal Apr 6 '17 at 22:26
  • In a .js file, it is a jquery script – Capiedge Apr 6 '17 at 22:36
  • downvoted as I suspect that anything that have to do with ACF needs to remain in ACF land, and ACF is off-topic.Personally I think that If you need flexibility ACF is your enemy, not your friend.... anyway If by miracle there is a vanilla wordpress solution that satisfies this, give me a ping and I will remove the downvote. – Mark Kaplun Apr 7 '17 at 3:14
  • @MarkKaplun forget about ACF, the main point of the question is to know how to access to some WP db data through js. It is independent of what plugin or function are you using... I'm used to achieve it coding php, but still a newbie when js is involved. – Capiedge Apr 7 '17 at 10:45
  • cool can you make some edit so I remove it, (if you care about it all) – Mark Kaplun Apr 7 '17 at 11:10
3

You would need to use the wp_localize_script() function which allows you to create a JavaScript object, fill it with data using PHP, and then pass it into the JavaScript file.

This is done in your functions.php file inside the function where you're enqueuing your JavaScript file. You will need to pass it the handle you enqueue your script as, the name of the object you want to use in JavaScript, and an array of properties and values for the object.

From then on you just use the wp_get_current_user() function to get your values.

For example:

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

   wp_enqueue_script( 'theme-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '', true );
   wp_localize_script( 'theme-script', 'theUser', array (
      'username' => $current_user->user_login,
   ) );
}
add_action( 'wp_enqueue_scripts', 'theme_scripts' );

The thing is that $current_user is not populated by default in your enqueue function, so you're going to need to call it somewhere before hand as follows:

global $current_user;
$current_user = wp_get_current_user();

This object can now be accessed in your script with properties for every key you put in the array. So in this case you would access the user's login name as theUser.username.

  • 1
    Thank you @belinus It works like a charm! I didn't know about wp_localize_script, and it seems to be a lifesaver for a variety of situations that I had never known how to solve, thanks again! – Capiedge Apr 7 '17 at 10:40
  • You're welcome. I did notice I forgot to add a bit to explain how to use the object so I edited it for future reference. – Cedon Apr 7 '17 at 15:18
  • 1
    I think that in the wp_localize_script declaration, theUser should be passed between quotation marks 'theUser', if not, I got an warning error message ;) – Capiedge Apr 7 '17 at 15:55
  • why are you using the $current_user global instead of just saving the results of wp_get_current_user() into a local variable? – shea Apr 19 '17 at 12:13
  • @shea When I was testing to make sure my answer worked, I dropped the global and it didn't work. – Cedon Apr 26 '17 at 18:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.