admin管理员组文章数量:1122832
I want to send user password reset link to their phone number instead of email. I can use Twillo to send sms process. I tried below code but there is an error. Any suggestions ?
function send_password_reset_sms( $user_login, $user_data ) {
$phone_number = get_user_meta( $user_data->ID, 'phone_number', true );
if ( $phone_number ) {
$reset_key = get_password_reset_key( $user_data );
$reset_url = network_site_url( "wp-login.php?action=rp&key=$reset_key&login=" . rawurlencode( $user_login ), 'login' );
// Use your SMS API to send the message
$message = "To reset your password, visit the following address: $reset_url";
// Example using Twilio
$sid = 'YOUR_TWILIO_SID';
$token = 'YOUR_TWILIO_AUTH_TOKEN';
$twilio_number = 'YOUR_TWILIO_PHONE_NUMBER';
$client = new \Twilio\Rest\Client($sid, $token);
$client->messages->create(
$phone_number,
array(
'from' => $twilio_number,
'body' => $message
)
);
}
}
add_action( 'retrieve_password', 'send_password_reset_sms', 10, 2 );
I want to send user password reset link to their phone number instead of email. I can use Twillo to send sms process. I tried below code but there is an error. Any suggestions ?
function send_password_reset_sms( $user_login, $user_data ) {
$phone_number = get_user_meta( $user_data->ID, 'phone_number', true );
if ( $phone_number ) {
$reset_key = get_password_reset_key( $user_data );
$reset_url = network_site_url( "wp-login.php?action=rp&key=$reset_key&login=" . rawurlencode( $user_login ), 'login' );
// Use your SMS API to send the message
$message = "To reset your password, visit the following address: $reset_url";
// Example using Twilio
$sid = 'YOUR_TWILIO_SID';
$token = 'YOUR_TWILIO_AUTH_TOKEN';
$twilio_number = 'YOUR_TWILIO_PHONE_NUMBER';
$client = new \Twilio\Rest\Client($sid, $token);
$client->messages->create(
$phone_number,
array(
'from' => $twilio_number,
'body' => $message
)
);
}
}
add_action( 'retrieve_password', 'send_password_reset_sms', 10, 2 );
Share
Improve this question
edited Jun 20, 2024 at 14:33
Pat J
12.3k2 gold badges28 silver badges36 bronze badges
asked Jun 20, 2024 at 11:03
BuddikaBuddika
111 bronze badge
1
- 1 I've done a few of these Twilio integrations, what's the error you get? – Tony Djukic Commented Jun 26, 2024 at 2:26
1 Answer
Reset to default 1To resolve the error in your code for sending a password reset link via SMS using Twilio, here’s a refined version of your function with necessary error handling and dependencies:
function send_password_reset_sms( $user_login, $user_data ) {
$phone_number = get_user_meta( $user_data->ID, 'phone_number', true );
if ( ! empty( $phone_number ) ) {
$reset_key = get_password_reset_key( $user_data );
if ( is_wp_error( $reset_key ) ) {
error_log( 'Error generating password reset key: ' . $reset_key->get_error_message() );
return;
}
$reset_url = network_site_url( "wp-login.php?action=rp&key=$reset_key&login=" . rawurlencode( $user_login ), 'login' );
$message = "To reset your password, visit the following address: $reset_url";
$sid = 'YOUR_TWILIO_SID';
$token = 'YOUR_TWILIO_AUTH_TOKEN';
$twilio_number = 'YOUR_TWILIO_PHONE_NUMBER';
if ( class_exists( '\Twilio\Rest\Client' ) ) {
$client = new \Twilio\Rest\Client($sid, $token);
try {
$client->messages->create(
$phone_number,
[
'from' => $twilio_number,
'body' => $message
]
);
} catch ( Exception $e ) {
error_log( 'Twilio Error: ' . $e->getMessage() );
}
} else {
error_log( 'Twilio SDK not found. Please include the Twilio PHP SDK in your project.' );
}
} else {
error_log( 'No phone number available for user ' . $user_login );
}
}
add_action( 'retrieve_password', 'send_password_reset_sms', 10, 2 );
Ensure you have the Twilio SDK installed (composer require twilio/sdk
) and check your server logs for error messages during testing.
本文标签: phpHow to send user password reset link to their phone number instead of email
版权声明:本文标题:php - How to send user password reset link to their phone number instead of email 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303154a1931786.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论