WooCommerce: Move Labels Inside Checkout Fields

Although UX and accessibility experts won’t like this customization, it’s still important to know “what’s possible” with WooCommerce.

In regard to the checkout form (billing + shipping + notes), there is a useful “woocommerce_checkout_fields” hook (filter) that is widely used by developers like me to alter the behavior of input fields.

In today’s episode we will take a look, indeed, at how to remove the checkout field labels from their default position (above fields), and use them as placeholders instead, so that we save up some vertical space.

Enjoy!

Once the snippet below is active, your WooCommerce checkout fields will look like this; field labels will now display inside their respective input fields.

PHP Snippet: Display Field Labels Inside Input Boxes @ Checkout Page

/**
 * @snippet       Move Labels Inside Inputs - WooCommerce Checkout
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 5
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_checkout_fields', 'bbloomer_labels_inside_checkout_fields', 9999 );
  
function bbloomer_labels_inside_checkout_fields( $fields ) {
	foreach ( $fields as $section => $section_fields ) {
		foreach ( $section_fields as $section_field => $section_field_settings ) {
			$fields[$section][$section_field]['placeholder'] = $fields[$section][$section_field]['label'];
			$fields[$section][$section_field]['label'] = '';
		}
	}
	return $fields;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

10 thoughts on “WooCommerce: Move Labels Inside Checkout Fields

  1. Hello,

    Could you show us how to edit the code so that we can assign this to specific fields, rather than all fields?

    1. Hi Gregory, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Awesome stuff! Now, if we can only get the asterisk to remain for required fields. ; )

  3. Hello,

    Possible to notify user if a field is required of not? As it looks like now it not very good UX. User do not know what fields are required and what are optional

    1. WooCommerce should already change the border to red for required fields, can you see that? Let me know

      1. I actually had the same question coming here. The missing fields are marked red if the user tries to checkout but he has no way of knowing in advance, which fields are considered required.
        I am guessing Ryan Logan had the same question/issue above.

        It seems to be a question of either having everything inline but no required field indicator or keeping everything as is to keep he asterisks. Is there any way out of this dilemma?

        1. Hey Michael, I take the placeholder could take an “*” if the field is required, or you could style field borders via CSS. I’m afraid this is custom work – if you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

          1. Hello all,
            For me, the following solution worked well. It adds an asterix for required fields and (optional) for not-required fields. You can change it to whatever you need in the code. Hope it works for you too.

            add_filter( 'woocommerce_checkout_fields', 'bbloomer_labels_inside_checkout_fields', 9999 );
            
            function bbloomer_labels_inside_checkout_fields( $fields ) {
            	foreach ( $fields as $section => $section_fields ) {
            		foreach ( $section_fields as $section_field => $section_field_settings ) {
            			if ($fields[$section][$section_field]['required'] == true) {
            				$fields[$section][$section_field]['placeholder'] = $fields[$section][$section_field]['label'] .= '*';
            			} else {
            				$fields[$section][$section_field]['placeholder'] = $fields[$section][$section_field]['label'] .= ' (optional)';
            			}
            		$fields[$section][$section_field]['label'] = '';
            		}
            	}
            	return $fields;
            }
Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *