Hola! I am

Freelancer Martin

WooCommerce is one of the most popular platforms for e-commerce, and it’s often necessary to add custom fields to the checkout page to collect extra information from your customers—such as a government ID. The example below shows how to add a new custom field to the WooCommerce checkout. It’s a simple and effective way to include this functionality in your own plugin.

add_action( 'woocommerce_init', function() {
woocommerce_register_additional_checkout_field( array(
'id' => 'namespace/gov-id',
'label' => 'Government ID',
'optionalLabel' => 'Government ID (optional)',
'location' => 'address',
'required' => true,
'attributes' => array(
'autocomplete' => 'government-id',
'aria-describedby' => 'some-element',
'aria-label' => 'custom aria label',
'pattern' => '[A-Z0-9]{5}', // 5 uppercase letters or digits
'title' => 'Title to show on hover',
'data-custom' => 'custom data',
),
) );
} );

What it does

Adds a new checkout field labeled “Government ID”

Marks it as required, so the customer cannot complete their order until it’s filled in

Uses a pattern attribute to enforce exactly five uppercase letters or digits

Includes ARIA attributes for improved accessibility and hints

How to use in your plugin

Create your WordPress plugin (or open your existing one).

Paste the code above into your main plugin file.

Make sure it’s hooked to woocommerce_init, so it runs when WooCommerce loads.

Activate your plugin in the WordPress admin.

Test the checkout page to confirm the new field appears and validates correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *