I am trying to pass username to database through form:

$current_user= wp_get_current_user();
$id= $current_user->user_login; 

<input type="hidden" name="id" value="[insert_php]$id[/insert_php]">

But it doesn't get through, the rest of visible inputs do go through though.


If you want to output the $id, you need to echo.
Also be sure to use esc_attr() as a good security practice.

<input type="hidden" name="id" value="[insert_php]echo esc_attr( $id );[/insert_php]">

In addition, as was noted in another answer, you're calling it an 'id', but you're actually using the login (username). If all you need is the numeric ID, use get_current_user_id().

<input type="hidden" name="id" value="[insert_php]echo esc_attr( get_current_user_id() );[/insert_php]">

Really, the answer by @jaswrks will work, if you are using a shortcode to run code via an edited page.

But, this is back to front way of using PHP.. and you are so close to learning how to really write PHP inside WordPress, instead of via a proxy like a shortcode that allows you to write quasi-PHP in the editor..

So, what you could also try, is to edit the page template where the form appears ( or add via widgets, blocks, external functions or any of the other means available ) and add the following:


// get current user ID, with default value, if empty
$current_user_id = wp_get_current_user_id() ?? 0;

<input type="hidden" name="id" value="<?php echo esc_attr( $current_user_id ); ?>">  

What you want is to use :

$id = get_current_user_id()

What the function that you are using does is get an object.

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.