Skip to content

Instantly share code, notes, and snippets.

@woogists
Created February 27, 2025 20:46
Show Gist options
  • Save woogists/d61cac8676c36383f802ed9aaffb5f52 to your computer and use it in GitHub Desktop.
Save woogists/d61cac8676c36383f802ed9aaffb5f52 to your computer and use it in GitHub Desktop.
Limit the number of characters that are accepted in the Gift Card Message field
<?php
/*
By default, customers are allowed to input as many characters as they’d like in the “Message” field of a Gift Card form.
To limit the number of characters that are accepted in this field, you can use this snippet.
Note: This snippet sets a limit to 300 characters. To set a different limit, it is necessary to modify the value of the "maxlength" attribute.
*/
add_action( 'init', 'sw_gc_set_message_limit' );
function sw_gc_set_message_limit() {
remove_action( 'woocommerce_gc_form_fields_html', 'wc_gc_form_field_message_html', 30 );
add_action( 'woocommerce_gc_form_fields_html', 'wc_gc_custom_form_field_message_html', 30 );
}
function wc_gc_custom_form_field_message_html() {
// Re-fill form.
$message = isset( $_REQUEST[ 'wc_gc_giftcard_message' ] ) ? sanitize_textarea_field( str_replace( '<br />', "\n", wp_unslash( wptexturize( urldecode( $_REQUEST[ 'wc_gc_giftcard_message' ] ) ) ) ) ) : ''; // @phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
?>
<div class="wc_gc_field wc_gc_giftcard_message">
<label for="wc_gc_giftcard_message"><?php esc_html_e( 'Message', 'woocommerce-gift-cards' ); ?></label>
<textarea maxlength="300" rows="3" name="wc_gc_giftcard_message" placeholder="<?php esc_attr_e( 'Add your message (optional)', 'woocommerce-gift-cards' ); ?>"><?php echo esc_html( $message ); ?></textarea>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment