search - How to fetch Email addres of an author via ID?

admin2025-01-07  4

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?

Share Improve this question edited Jul 16, 2021 at 4:44 Sanu_012 asked Jul 16, 2021 at 4:37 Sanu_012Sanu_012 1037 bronze badges 3
  • "I made a new php file in the root folder of my WP directory and put in this code" - why did you redeclare or manually define the 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 like admin_post_ (e.g. admin_post_action-name for logged-in users) or template_redirect to properly handle form submissions in WordPress. – Sally CJ Commented Jul 16, 2021 at 14:12
  • This didn't help. Can you explain a bit more. I am totally a beginner. – Sanu_012 Commented Jul 18, 2021 at 12:28
  • I was just saying, your form should not submit to a static PHP file outside of WP environment - if you had to manually load core WP files, then you're basically outside the WP environment. So perhaps you can try creating a custom page template and put your form code there (e.g. <form method="post" action="">your form fields</form>), and then you can test the get_user_by() function, e.g. add var_dump( get_user_by( 'id', 53 ) ); somewhere in your template. – Sally CJ Commented Jul 19, 2021 at 8:57
Add a comment  | 

1 Answer 1

Reset to default 0

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;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736259942a627.html

最新回复(0)