How to Validate Payment Gateway Settings Fields
It seems like my complete payment gateway tutorial is not that complete after all. I didn’t mention there how you can validate payment gateway admin options.
For example let’s assume that we have to validate a USDT address of a TRON network. Here is how it should work:

Let’s assume that we’ve already added a field for a USDT address inside init_form_fields()
method. Just like that:
public function init_form_fields(){
$this->form_fields = array(
...
'trc20_address' => array(
'title' => 'USDT address',
'type' => 'text',
),
);
}
There are actually two ways how we can validate this custom option field:
- We can add
process_admin_options()
method to our payment gateway class. Inside this method we can obtain the$_POST
data with$this->get_post_data()
method and then validate its particular array elements. - Or simply use
validate_{NAME}_field()
method. I prefer this solution because it looks much cleaner in the code.
public function validate_trc20_address_field( $key, $value ) {
if ( ! preg_match( '/^T[A-Za-z1-9]{33}$/', $value ) ) {
WC_Admin_Settings::add_error( 'The wallet address doesn\'t look like a correct one.' );
$value = ''; // empty it because it is not correct
}
return $value;
}
I used a regular expression T[A-Za-z1-9]{33}
in order to validate the USDT address.

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me