wp_sms_otp_generated
Action hook triggered when an OTP is generated, useful for sending notifications.
The wp_sms_otp_generated action fires whenever a One-Time Password (OTP) is generated. Use this hook to perform custom actions such as sending email notifications or logging OTP events.
Syntax
add_action('wp_sms_otp_generated', 'your_callback', 10, 3);
Parameters
| Parameter | Type | Description |
|---|---|---|
$otp | string | The generated OTP code |
$phoneNumber | string | The phone number associated with the OTP |
$agent | string | The agent/source that triggered the OTP |
Example
Send an email notification when an OTP is generated:
add_action('wp_sms_otp_generated', 'send_otp_email_notification', 10, 3);
function send_otp_email_notification($otp, $phoneNumber, $agent) {
// Get user by phone number
$user = get_users(array(
'meta_key' => 'mobile',
'meta_value' => $phoneNumber,
'number' => 1,
));
if (empty($user)) {
return;
}
$user = $user[0];
$to = $user->user_email;
$subject = 'Your OTP Code';
$message = sprintf(
"An OTP has been generated for your account.\n\nOTP: %s\nPhone: %s\nSource: %s",
$otp,
$phoneNumber,
$agent
);
wp_mail($to, $subject, $message);
}
Use Cases
- Send email backup when OTP is generated
- Log OTP generation for security auditing
- Notify administrators of OTP activity
- Integrate with external logging systems
Related
- wp_sms_add_subscriber - Action hook for new subscribers
Last updated: December 23, 2024