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
Add a comment  | 

1 Answer 1

Reset to default 1

To 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