I want to make an HTML form which will have an input field to fill in the ID of the user. Then I want to pass the value into a php code such that the email address associated with that user ID is displayed. However, I am new to WP and don't know how to do it correctly.
I made a new php file in the root folder of my WP directory and put in this code and tried by browsing the file, but it didn't work.
<?php
$field = 53;
$value = 53;
function get_user_by( $field, $value ) {
$userdata = WP_User::get_data_by( $field, $value );
if ( ! $userdata ) {
return false;
}
$user = new WP_User;
$user->init( $userdata );
var_dump($user);
}
?>
I searched in the web as well, but couldn't find anything of this sort? Can someone guide me through this?
I want to make an HTML form which will have an input field to fill in the ID of the user. Then I want to pass the value into a php code such that the email address associated with that user ID is displayed. However, I am new to WP and don't know how to do it correctly.
I made a new php file in the root folder of my WP directory and put in this code and tried by browsing the file, but it didn't work.
<?php
$field = 53;
$value = 53;
function get_user_by( $field, $value ) {
$userdata = WP_User::get_data_by( $field, $value );
if ( ! $userdata ) {
return false;
}
$user = new WP_User;
$user->init( $userdata );
var_dump($user);
}
?>
I searched in the web as well, but couldn't find anything of this sort? Can someone guide me through this?
Use get_userdata()
to get a user object, and the email address will be the email
property:
$userdata = get_userdata( $user_id );
if ( ! $userdata ) {
return false;
}
echo $userdata->email;
get_user_by()
function? Does your form submit to that PHP file (e.g.<form action="https://example.com/some-file.php">
)? If so, then you shouldn't do that. Instead, use hooks likeadmin_post_
(e.g.admin_post_action-name
for logged-in users) ortemplate_redirect
to properly handle form submissions in WordPress. – Sally CJ Commented Jul 16, 2021 at 14:12<form method="post" action="">your form fields</form>
), and then you can test theget_user_by()
function, e.g. addvar_dump( get_user_by( 'id', 53 ) );
somewhere in your template. – Sally CJ Commented Jul 19, 2021 at 8:57