How to Implement PAN or Aadhaar Login Field

Recent Blog

Ready to Transform Your Business? Contact Us Today

Screenshot 2025 01 22 120200

Table of Contents

Introduction:

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.

Step 1: Setting Up the Environment

The Impact of Too Much Content

Before diving into the code, ensure you have access to your WordPress files and database:

  1. Access Files: Use an FTP client like FileZilla or your hosting provider’s File Manager to access WordPress files.

  2. Backup: Backup the functions.php file and the database to prevent data loss during the process.

Screenshot 2025 01 22 120200

Step 2: Adding Custom Fields for PAN and Aadhaar

To store PAN and Aadhaar numbers, you need to extend the user profile. This requires adding custom fields during registration.

Screenshot 2025 01 22 120422

Code to Add Custom Fields:

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’]));
}
}

What This Does:

  • Adds input fields for PAN and Aadhaar during user registration.

  • Saves the data to the WordPress wp_usermeta table.

Step 3: Validating PAN and Aadhaar Formats

To ensure the entered PAN or Aadhaar numbers are valid, you’ll use server-side validation.

Screenshot 2025 01 22 120551

Code for 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;
}

What This Does:

  • Checks if the entered PAN matches the format ABCDE1234F (5 letters, 4 digits, 1 letter).

  • Validates Aadhaar to ensure it contains exactly 12 digits.

Step 4: Allowing Login with PAN or Aadhaar

Customize the WordPress login process to accept PAN or Aadhaar as a username.

Code for Login Modification:

  • Add this to functions.php:

Screenshot 2025 01 22 120746

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;
}

What This Does:

  • Matches the entered PAN or Aadhaar number against the database.

  • Authenticates the user if a match is found.

Step 5: Adding API-Based Validation (Optional)

For additional security, validate PAN and Aadhaar numbers using external APIs.

Screenshot 2025 01 22 121404

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;
}

Integrate this function into registration and login processes for server-side validation.

Step 6: Customize the Login Page

Guide users to log in using their PAN or Aadhaar number.

Code:

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;
}

Screenshot 2025 01 22 121512

Step 7: Testing and Deployment

  1. Test Registration: Ensure PAN/Aadhaar fields are saved correctly.

  2. Test Login: Verify users can log in with PAN or Aadhaar.

  3. Error Handling: Check error messages for invalid inputs or API failures.

  4. Data Security: Ensure data is transmitted over HTTPS and sensitive information is not logged.


Screenshot 2025 01 22 121619

Conclusion: Take An Appropriate Action

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.

Picture of Rohit Singh

Rohit Singh

Leave a Comment

Think about well design website

Let Us Take Care Of Your website

Let’s do great things with tech together!

Get 15 % discount for 🤘 up

Schedule Appointment

Fill out the form below, and we will be in touch within 24 hrs.

Contact Information
Vehicle Information
Preferred Date and Time Selection