Getting information of Logged in user in wordpress

29 Views Asked by At

I have created a website in Wordpress. In that , I has a page create_invoice, where I have added html that takes user input in the form.

I have set up the logic so that this page is only displayed to logged in user.

What I want is, when user submits the form, I want to send a mail to the logged in user with a pdf of the invoice.

I have generated code to send the email. But I cannot get email of the logged in user via javascript.

I have checked on many sources online , like Wordpress Codex and stackoverflow, but all suggests functions of php like get_current_user_id() etc.

But I cannot figure out how to use php in my code, and even when I display the userinfo using php , i get notting. <?php $userinfo = get_current_user_id(); echo '.$userinfo.' ?>

Can any one please help me out in this. Or can I use some sort of rest api to get logged in user info.

1

There are 1 best solutions below

2
Lajos Arpad On

This is how you can get the current Wordpress user in PHP:

$current_user = wp_get_current_user();
$user_email = $current_user->user_email;
echo "Current User Email: " . $user_email;

and this is how you can put it into Javascript:

<script>
    let email = "<?php echo htmlspecialchars($user_email, ENT_QUOTES, 'UTF-8'); ?>";
</script>