Location Details Meta Box
Adds address, phone, opening hours and map coordinates to locations and a [location_details] shortcode with a click-to-call link and directions.
Opens WPIDE on
Code
// Post type, and the fields: meta key (saved with a leading underscore) => label and input type.
$wpide_box = array(
'post_type' => 'location',
'title' => 'Address and hours',
'fields' => array(
'location_street' => array( 'label' => 'Street address', 'type' => 'text' ),
'location_city' => array( 'label' => 'City', 'type' => 'text' ),
'location_region' => array( 'label' => 'State or region', 'type' => 'text' ),
'location_postcode' => array( 'label' => 'Postcode', 'type' => 'text' ),
'location_country' => array( 'label' => 'Country code (e.g. US)', 'type' => 'text' ),
'location_phone' => array( 'label' => 'Phone', 'type' => 'tel' ),
'location_hours' => array( 'label' => 'Opening hours (one per line)', 'type' => 'textarea' ),
'location_lat' => array( 'label' => 'Latitude', 'type' => 'text' ),
'location_lng' => array( 'label' => 'Longitude', 'type' => 'text' ),
),
);
add_action( 'add_meta_boxes_' . $wpide_box['post_type'], function () use ( $wpide_box ) {
add_meta_box( 'wpide-location-details', $wpide_box['title'], function ( $post ) use ( $wpide_box ) {
wp_nonce_field( 'wpide_location_details', 'wpide_location_details_nonce' );
echo '<div style="display:grid;gap:12px;grid-template-columns:repeat(auto-fill,minmax(220px,1fr))">';
foreach ( $wpide_box['fields'] as $key => $field ) {
$value = get_post_meta( $post->ID, '_' . $key, true );
$id = 'wpide-' . str_replace( '_', '-', $key );
echo '<p style="margin:0">';
if ( 'checkbox' === $field['type'] ) {
printf( '<label><input type="checkbox" name="%1$s" value="1"%2$s> %3$s</label>', esc_attr( $key ), checked( $value, '1', false ), esc_html( $field['label'] ) );
} else {
printf( '<label for="%1$s"><strong>%2$s</strong></label><br>', esc_attr( $id ), esc_html( $field['label'] ) );
if ( 'textarea' === $field['type'] ) {
printf( '<textarea class="widefat" rows="5" id="%1$s" name="%2$s">%3$s</textarea>', esc_attr( $id ), esc_attr( $key ), esc_textarea( $value ) );
} elseif ( 'select' === $field['type'] ) {
printf( '<select class="widefat" id="%1$s" name="%2$s">', esc_attr( $id ), esc_attr( $key ) );
foreach ( $field['options'] as $option => $label ) {
printf( '<option value="%1$s"%2$s>%3$s</option>', esc_attr( $option ), selected( (string) $value, (string) $option, false ), esc_html( $label ) );
}
echo '</select>';
} else {
printf( '<input class="widefat" type="%1$s" id="%2$s" name="%3$s" value="%4$s"%5$s>', esc_attr( $field['type'] ), esc_attr( $id ), esc_attr( $key ), esc_attr( $value ), 'number' === $field['type'] ? ' step="any" min="0"' : '' );
}
}
echo '</p>';
}
echo '</div>';
}, $wpide_box['post_type'], 'normal', 'high' );
} );
add_action( 'save_post_' . $wpide_box['post_type'], function ( $post_id ) use ( $wpide_box ) {
if ( ! isset( $_POST['wpide_location_details_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wpide_location_details_nonce'] ), 'wpide_location_details' ) ) {
return;
}
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
foreach ( $wpide_box['fields'] as $key => $field ) {
$raw = isset( $_POST[ $key ] ) ? wp_unslash( $_POST[ $key ] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- sanitized below per type.
$raw = is_string( $raw ) ? $raw : '';
switch ( $field['type'] ) {
case 'url':
$value = esc_url_raw( $raw );
break;
case 'email':
$value = sanitize_email( $raw );
break;
case 'number':
$value = is_numeric( $raw ) ? (string) ( 0 + $raw ) : '';
break;
case 'textarea':
$value = sanitize_textarea_field( $raw );
break;
case 'checkbox':
$value = $raw ? '1' : '';
break;
case 'select':
$value = array_key_exists( $raw, $field['options'] ) ? $raw : '';
break;
case 'date':
$value = preg_match( '/^\d{4}-\d{2}-\d{2}$/', $raw ) ? $raw : '';
break;
case 'datetime-local':
$value = preg_match( '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/', $raw ) ? $raw : '';
break;
default:
$value = sanitize_text_field( $raw );
}
if ( '' === $value ) {
delete_post_meta( $post_id, '_' . $key );
} else {
update_post_meta( $post_id, '_' . $key, $value );
}
}
} );
add_shortcode( 'location_details', function () {
$id = get_the_ID();
if ( ! $id || 'location' !== get_post_type( $id ) ) {
return '';
}
$get = function ( $key ) use ( $id ) { return trim( (string) get_post_meta( $id, '_location_' . $key, true ) ); };
$address = implode( ', ', array_filter( array( $get( 'street' ), $get( 'city' ), trim( $get( 'region' ) . ' ' . $get( 'postcode' ) ), $get( 'country' ) ) ) );
$html = '<div class="location-details">';
if ( $address ) {
$dest = ( $get( 'lat' ) && $get( 'lng' ) ) ? $get( 'lat' ) . ',' . $get( 'lng' ) : $address;
$html .= '<p class="location-address">' . esc_html( $address ) . '<br><a href="' . esc_url( add_query_arg( array( 'api' => 1, 'destination' => $dest ), 'https://www.google.com/maps/dir/' ) ) . '" target="_blank" rel="noopener">Get directions</a></p>';
}
if ( $get( 'phone' ) ) {
$html .= '<p class="location-phone"><a href="tel:' . esc_attr( preg_replace( '/[^\d+]/', '', $get( 'phone' ) ) ) . '">' . esc_html( $get( 'phone' ) ) . '</a></p>';
}
$hours = array_filter( array_map( 'trim', explode( "\n", $get( 'hours' ) ) ) );
if ( $hours ) {
$html .= '<ul class="location-hours"><li>' . implode( '</li><li>', array_map( 'esc_html', $hours ) ) . '</li></ul>';
}
return $html . '</div>';
} );
Notes
Enter opening hours one per line, e.g. "Mo-Fr 09:00-17:00". The LocalBusiness-per-location schema snippet reads these fields.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.