I am creating a custom script where when successfully sign up, it will pass data to a third party api curl.
The sign up flow using digits plugin currently are, user will be prompted to insert their contact number, email, name and password and upon clicking continue button, user will be prompted to verify by insert the otp code send to their phone number via sms. And upon success, their register data will be passed to the third party api curl and also user data are inserted into the wordpress table.
So what I need to do now is, before user can proceed to insert the otp code/before the user data are created in the wordpress table, I need to first verify if their data already exist in the third party system. If their data already exist then I will need to prompt error and user can’t proceed with the registration/their data will not be created in the wordpress table.
In my script file, the code are as below inside custom-hook.php
file;
<?php
add_action('digits_before_register_user', 'custom_digits_before_register', 1, 1);
function log_debug($process_name, $reference_id = null, $request, $response, $status = null, $updated_at = '0000-00-00 00:00:00' )
{
global $wpdb;
$wpdb->insert('log_hook', [
'process_name' => $process_name, // This can be the function name, etc.
'reference_id' => $reference_id, // This can be the order ID, customer ID, etc.
'request' => $request, // The request message
'response' => $response, // The response message
'status' => $status, // The status of the process 1 = success, 0 = failed
'created_at' => CURRENT_DATETIME,
'updated_at' => $updated_at
]);
}
function custom_digits_before_register($user_data) {
// Extract data from the registration process
$data = [
'phone_number' => $user_data['countrycode'] . $user_data['mobile'],
'email' => $user_data['email'],
];
// Log: for before register user details
log_debug('custom_digits_before_register', '', '', json_encode($data), 1);
wp_send_json_error(['message' => 'Unable to validate data at this time. Please try again later.']);
exit;
}
log_debug
is a function that insert data into the log table for debugging purpose.
When I tried the register flow with above hook, after insert the details (contact number, email, name and password) and then click on the continue button, the otp number are still sent to my number and the hook are not triggered as no log data are inserted in the log table. Since the hook not triggering, so I cannot proceed with the checking with third party api.
Aside from digits_before_register_user
, I also tried digits_before_otp_verification
, digits_validate_otp
and digits_custom_validation
suggested by chatgpt but also not working.
I tried to check their documentation at documentation link but it shows error.
Would really appreciate if someone have experience of using this plugin. I don’t know the structure of this plugin’s code. And I only have access to the custom-hook.php
file to add function here.
Or if there are any other workaround that didn’t cross my mind would also be appreciated.
Thank you in advance!
Edit:
Here is the create table query for log_hook
CREATE TABLE `log_hook` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`process_name` longtext DEFAULT NULL,
`reference_id` int(11) DEFAULT NULL COMMENT 'example user_id/order_id/member_code etc for reference',
`request` longtext DEFAULT NULL,
`response` longtext DEFAULT NULL,
`status` int(11) DEFAULT NULL COMMENT '0: failed, 1: success',
`created_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;
2
Answers
After looking into the Digits plugin files, I found the hook/filters that can trigger before user registration data is inserted into the WordPress database and before the OTP is sent to the user's SMS.
the hook/filters can be found in below file path
So, inside the custom function here, you can insert the custom checking. The user will not be able to proceed to receive and verify OTP if this function returns an error.
I have tested this hook/filters and it works.
Use below the code. There is no
add_action('digits_before_register_user', 'custom_digits_before_register', 1, 1);
hook in that plugininstead of