Blog/ Single Post
Adding a custom login field in WordPress that uses PAN (Permanent Account Number) or Aadhaar numbers provides a unique way for users to log in. This guide will take you through the step-by-step process of implementing this feature, including server-side validation through APIs. By the end of this article, you’ll have a fully functional solution to allow users to register and log in with their PAN or Aadhaar number in WordPress.
Before diving into the code, ensure you have access to your WordPress files and database:
Access Files: Use an FTP client like FileZilla or your hosting provider’s File Manager to access WordPress files.
Backup: Backup the functions.php
file and the database to prevent data loss during the process.
To store PAN and Aadhaar numbers, you need to extend the user profile. This requires adding custom fields during registration.
Add the following to your theme’s functions.php
file:
// Add custom fields during registration
add_action(‘register_form’, ‘custom_registration_fields’);
function custom_registration_fields() {
?>
<p>
<label for=”pan_number”>PAN Number (Optional)<br/>
<input type=”text” name=”pan_number” id=”pan_number” class=”input” size=”25″ /></label>
</p>
<p>
<label for=”aadhar_number”>Aadhaar Number (Optional)<br/>
<input type=”text” name=”aadhar_number” id=”aadhar_number” class=”input” size=”25″ /></label>
</p>
<?php
}
// Save custom fields during registration
add_action(‘user_register’, ‘save_custom_registration_fields’);
function save_custom_registration_fields($user_id) {
if (!empty($_POST[‘pan_number’])) {
update_user_meta($user_id, ‘pan_number’, sanitize_text_field($_POST[‘pan_number’]));
}
if (!empty($_POST[‘aadhar_number’])) {
update_user_meta($user_id, ‘aadhar_number’, sanitize_text_field($_POST[‘aadhar_number’]));
}
}
Adds input fields for PAN and Aadhaar during user registration.
Saves the data to the WordPress wp_usermeta
table.
To ensure the entered PAN or Aadhaar numbers are valid, you’ll use server-side validation.
Append this code to functions.php
:
// Validate PAN and Aadhaar during registration
add_filter(‘registration_errors’, ‘validate_pan_aadhar_fields’, 10, 3);
function validate_pan_aadhar_fields($errors, $sanitized_user_login, $user_email) {
if (!empty($_POST[‘pan_number’]) && !preg_match(‘/^[A-Z]{5}[0-9]{4}[A-Z]{1}$/’, $_POST[‘pan_number’])) {
$errors->add(‘invalid_pan’, ‘Invalid PAN number format. It should be like ABCDE1234F.’);
}
if (!empty($_POST[‘aadhar_number’]) && !preg_match(‘/^\d{12}$/’, $_POST[‘aadhar_number’])) {
$errors->add(‘invalid_aadhaar’, ‘Invalid Aadhaar number format. It should be 12 digits.’);
}
return $errors;
}
Checks if the entered PAN matches the format ABCDE1234F (5 letters, 4 digits, 1 letter).
Validates Aadhaar to ensure it contains exactly 12 digits.
Customize the WordPress login process to accept PAN or Aadhaar as a username.
Add this to functions.php
:
add_filter(‘authenticate’, ‘login_with_pan_or_aadhar’, 20, 3);
function login_with_pan_or_aadhar($user, $username, $password) {
if (empty($username)) {
return $user;
}
// Check if the username is PAN or Aadhaar
$meta_key = ”;
if (preg_match(‘/^[A-Z]{5}[0-9]{4}[A-Z]{1}$/’, $username)) {
$meta_key = ‘pan_number’;
} elseif (preg_match(‘/^\d{12}$/’, $username)) {
$meta_key = ‘aadhar_number’;
}
if (!empty($meta_key)) {
$users = get_users([
‘meta_key’ => $meta_key,
‘meta_value’ => $username,
‘number’ => 1,
‘count_total’ => false,
]);
if (!empty($users)) {
$user = $users[0];
} else {
return new WP_Error(‘invalid_login’, ‘Invalid PAN or Aadhaar number.’);
}
}
return $user;
}
Matches the entered PAN or Aadhaar number against the database.
Authenticates the user if a match is found.
function validate_pan_api($pan_number) {
$api_url = ‘https://api.example.com/validate-pan’;
$api_key = ‘your_api_key_here’;
$response = wp_remote_post($api_url, [
‘body’ => json_encode([‘pan’ => $pan_number]),
‘headers’ => [
‘Content-Type’ => ‘application/json’,
‘Authorization’ => ‘Bearer ‘ . $api_key,
],
]);
if (is_wp_error($response)) {
return new WP_Error(‘api_error’, ‘Could not connect to the PAN validation service.’);
}
$response_body = wp_remote_retrieve_body($response);
$data = json_decode($response_body, true);
return isset($data[‘valid’]) && $data[‘valid’] === true;
}
add_filter(‘login_message’, ‘customize_login_message’);
function customize_login_message($message) {
return ‘<p class=”message”>You can log in using your PAN or Aadhaar number.</p>’ . $message;
}
Test Registration: Ensure PAN/Aadhaar fields are saved correctly.
Test Login: Verify users can log in with PAN or Aadhaar.
Error Handling: Check error messages for invalid inputs or API failures.
Data Security: Ensure data is transmitted over HTTPS and sensitive information is not logged.
Integrating PAN or Aadhaar-based login functionality into WordPress is a practical and user-friendly feature that enhances both convenience and security. By following this guide, you’ve learned how to add custom fields for PAN and Aadhaar, validate their formats, and optionally incorporate external APIs for enhanced verification. This solution not only streamlines the login process for users but also ensures their data is handled securely and efficiently.
While implementing this functionality, remember to prioritize compliance with local regulations, especially regarding sensitive data like Aadhaar numbers. Testing each step thoroughly is essential to ensure a seamless user experience.
“Where exceptional design meets innovative web technology. Empowering your digital journey, one click at a time.”
Copyright 2024 © All Rights Reserved I Design by Mudita Technicals
How can We help You?
🟢 Online | Privacy policy