How to access user password hash from wordpress plugin?

I am trying to write a plugin that writes a users password hash to an external database. While debugging I have found that the following code causes user registration to not load once they have entered their email address and username.

function add_user_to_radius_db($user_id) {
// get the users password from the wordpress db
global $wpdb;

// writing a log file to test the parameters of my script
$myFile = fopen(“debug.log”, “w”) or die(“Unable to write file”);

$password = $wpdb->get_results(‘SELECT user_pass FROM ‘ . $wpdb->users . ‘ WHERE ID=’ . $user_id);
$user_info = get_userdata($user_id);

// if i add this line the registartion doesn’t load after entering the details
// if I remove it, the $password varible doesn’t get written to a file due to it being an array
$password = implode(”, $password);

$pass_str = “Password: ” . $password . “n”;
$uname_str = “Username: ” . $user_info->user_login . “n”;
$email_str = “Email: ” . $user_info->user_email . “n”;

fwrite($myFile, $pass_str);
fwrite($myFile, $uname_str);
fwrite($myFile, $email_str);

fclose($myFile);

// changed for security reasons
$conn = new mysqli(‘database_hostname.com’, ‘db_user’, ‘db_password’, ‘my_database’);

if (!$conn) {
die(“database connection failed”);
}

$sql = “INSERT INTO table (username, value, email, first_name, last_name, role) VALUES (” . $user_info->user_login . “, ” . $password . “, “. $user_info->user_email . “, ” . $user_info->first_name . “, ”
. $user_info->last_name . “, ‘user'”;

if (!$conn->query($sql) === true) {
echo “Error: ” . $sql . “<br>” . $conn->error;
}

$conn->close();
}
add_action(‘user_register’, ‘add_user_to_radius_db’);

When removing the line containing the implode function the pluging works but the password field is blank but when i include it the user registation form doesn’t load after clicking submit and the sign up email is never sent.

Read more here:: How to access user password hash from wordpress plugin?

Leave a Reply

Your email address will not be published. Required fields are marked *