Master Etch with tutorials, tips, and tricks.

Search

Recent articles

Meta Box: Creating a Select Label Field

  • Dynamic Data
  • Meta Box

One of the things I like about the Bricks + Meta Box integration is that select fields expose both the saved value and the human-readable label. For example, a State select configured as MD : Maryland makes both data points available dynamically.

Unfortunately, this is not currently the case in Etch. Meta Box only stores the selected value (MD), not the label (Maryland).

To solve this problem, I created a small PHP snippet that writes the label text to a separate custom field whenever the post is saved. In my example, the State select field (location_state) writes the matching label to another field called location_state_label.

This makes the readable label available anywhere in Etch using dynamic data such as:

item.metabox.location_state_label

This approach is especially useful for:

  • State abbreviations
  • Status fields
  • Dropdowns with short stored values
  • Any select/radio field where the label is more meaningful for front-end display

It also keeps the original Meta Box field intact while giving Etch access to the display-friendly version of the data.

Here is the PHP

<?php 

add_action( 'rwmb_after_save_post', function( $post_id ) {
	if ( get_post_type( $post_id ) !== 'venues' ) {
		return;
	}

	$states = [
		'AL' => 'Alabama',
		'AK' => 'Alaska',
		'AZ' => 'Arizona',
		'AR' => 'Arkansas',
		'CA' => 'California',
		'CO' => 'Colorado',
		'CT' => 'Connecticut',
		'DC' => 'District of Columbia',
		'DE' => 'Delaware',
		'FL' => 'Florida',
		'GA' => 'Georgia',
		'HI' => 'Hawaii',
		'ID' => 'Idaho',
		'IL' => 'Illinois',
		'IN' => 'Indiana',
		'IA' => 'Iowa',
		'KS' => 'Kansas',
		'KY' => 'Kentucky',
		'LA' => 'Louisiana',
		'ME' => 'Maine',
		'MD' => 'Maryland',
		'MA' => 'Massachusetts',
		'MI' => 'Michigan',
		'MN' => 'Minnesota',
		'MS' => 'Mississippi',
		'MO' => 'Missouri',
		'MT' => 'Montana',
		'NE' => 'Nebraska',
		'NV' => 'Nevada',
		'NH' => 'New Hampshire',
		'NJ' => 'New Jersey',
		'NM' => 'New Mexico',
		'NY' => 'New York',
		'NC' => 'North Carolina',
		'ND' => 'North Dakota',
		'OH' => 'Ohio',
		'OK' => 'Oklahoma',
		'OR' => 'Oregon',
		'PA' => 'Pennsylvania',
		'RI' => 'Rhode Island',
		'SC' => 'South Carolina',
		'SD' => 'South Dakota',
		'TN' => 'Tennessee',
		'TX' => 'Texas',
		'UT' => 'Utah',
		'VT' => 'Vermont',
		'VA' => 'Virginia',
		'WA' => 'Washington',
		'WV' => 'West Virginia',
		'WI' => 'Wisconsin',
		'WY' => 'Wyoming',
		'AS' => 'American Samoa',
		'GU' => 'Guam',
		'MP' => 'Northern Mariana Islands',
		'PR' => 'Puerto Rico',
		'UM' => 'United States Minor Outlying Islands',
		'VI' => 'Virgin Islands',
	];

	$value = get_post_meta( $post_id, 'location_state', true );

	if ( isset( $states[ $value ] ) ) {
		update_post_meta( $post_id, 'location_state_label', $states[ $value ] );
		return;
	}

	delete_post_meta( $post_id, 'location_state_label' );
});

Share Your Thoughts

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