wc_sanitize_coupon_code() │ WC 3.6.0

Sanitize a coupon code.

Coupon codes are plain text, but are stored as post titles with &, < and > encoded the same way wp_kses() did, so codes saved by earlier versions still match. Kses and the post title filters aren't used, so the result doesn't depend on the current user or on how wp_kses() parses HTML. Use the woocommerce_coupon_code filter to change codes.

No Hooks.

Returns

string.

Usage

wc_sanitize_coupon_code( $value );
$value(string|null) (required)
Coupon code to format.

Notes

See: WC_Formatting_Functions_Test::test_wc_sanitize_coupon_code
See: WC_Cart_Test::test_coupon_codes_with_special_characters

Changelog

Since 3.6.0 Introduced.
Since 10.0.0 Decode HTML entities here instead of via woocommerce_coupon_code filter.
Since 11.3.0 Encode special characters without kses or the post title filters, so the result no longer depends on the current user.

wc_sanitize_coupon_code() code WC 11.3.0

function wc_sanitize_coupon_code( $value ) {
	if ( null === $value ) {
		return '';
	}

	$value = html_entity_decode( $value, ENT_COMPAT, get_bloginfo( 'charset' ) );
	$value = trim( wp_kses_no_null( $value, array( 'slash_zero' => 'keep' ) ) );
	return str_replace( array( '<', '>' ), array( '&lt;', '&gt;' ), wp_kses_normalize_entities( $value ) );
}