Limit the gift card message field in the browser

Set a browser-side character limit for the gift card message field. This does not enforce a server-side limit.

Note:

This is a Developer level doc. If you are unfamiliar working with code and resolving potential conflicts, we recommend you work with a Woo Agency Partner for larger projects, or find a WooCommerce developer on Codeable for smaller customizations. We are unable to provide support for customizations under our Support Policy.

Test custom code on a staging site before using it on your store. Add PHP to a custom plugin or a child theme’s functions.php file. If the file is already inside a PHP block, omit the opening PHP tag.

This example limits the message field to 300 characters in the browser. Change maxlength to use a different limit. This does not prevent a longer message from being submitted outside the form.

<?php
add_action( 'init', 'sw_gc_set_message_limit' );

/**
 * Replace the message field with a browser-limited version.
 */
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', 'sw_gc_limited_message_field', 30 );
}

/**
 * Render the gift card message field.
 */
function sw_gc_limited_message_field() {
	$message = sanitize_textarea_field( WC_GC_Gift_Card_Product::get_form_field_value( 'wc_gc_giftcard_message' ) );
	?>
	<div class="wc_gc_field wc_gc_giftcard_message form-row">
		<label for="wc_gc_giftcard_message"><?php esc_html_e( 'Message', 'woocommerce-gift-cards' ); ?></label>
		<textarea maxlength="300" rows="3" class="input-text" id="wc_gc_giftcard_message" name="wc_gc_giftcard_message" aria-describedby="wc_gc_giftcard_message_description"><?php echo esc_html( $message ); ?></textarea>
		<small id="wc_gc_giftcard_message_description" class="description"><?php esc_html_e( 'Add your message (optional).', 'woocommerce-gift-cards' ); ?></small>
	</div>
	<?php
}

Verify the change

↑ Back to top

Enter a message in the product form and confirm the browser field stops at the configured limit. This does not enforce a server-side limit.

Questions and support

↑ Back to top

Something missing from this documentation? Still have questions and need assistance?

  • If you have a question about a specific extension or theme you’d like to purchase, contact us to get answers.
  • If you already purchased this product and need some assistance, get in touch with a Happiness Engineer via our support page and select this product’s name from the Product dropdown.

Related Products

Offer Royal Mail shipping rates to your customers.

Notify customers when your products are restocked.

Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.