The WooCommerce Settings API documentation gives just enough information to get settings added to Gateway/Shipping Method screens.
However, unlike the native WordPress Settings API, there are no documented “sanitize” callbacks, like WordPress’ register_setting()
has. Thus, if invalid content is supplied, I’m not sure where I’m supposed to be able to use add_settings_error
or similar.
How am I supposed to do this, and what documentation did I overlook?
Not sure where it’s documented (if at all) but I found an answer in code:
woocommerce/classes/abstracts/abstract-wc-settings-api.php
~ 651 (in 2.0ish) has the following code:Which means that if you added a setting with something like this:
Then you’d need to add a method on the same payment gateway class called
validate_username_field
, that takes a single argument (the setting value; in this case the username) and returns the sanitized value and/or creates an error.E.g.,
Or something along those lines.