Skip to content

Instantly share code, notes, and snippets.

@tanftw
Last active November 30, 2021 05:10
Show Gist options
  • Save tanftw/b626e45546223df55a24 to your computer and use it in GitHub Desktop.
Save tanftw/b626e45546223df55a24 to your computer and use it in GitHub Desktop.
Meta Box Geolocation Example
<?php
/**
* This is Meta Box Geolocation usage example.
* Include this file to your plugin or theme or see the usage guide below to apply to your Meta Box.
*/
add_filter( 'rwmb_meta_boxes', function( $meta_boxes )
{
$meta_boxes[] = array(
'id' => 'geolocation',
'title' => 'GeoLocation',
'context' => 'normal',
'priority' => 'high',
// Tell WP this Meta Box is GeoLocation
'geo' => true,
// Or you can set advanced settings for Geo, like this example:
// Restrict results to Australia only.
// Uncomment to use it.
// 'geo' => array(
// 'componentRestrictions' => array(
// 'country' => 'au'
// )
// ),
'fields' => array(
// Set the ID to `address` or `address_something` to make Auto Complete field
array(
'type' => 'text',
'name' => 'Address',
'id' => 'address'
),
// Auto populate `postal_code` to this field
array(
'type' => 'number',
'name' => 'Postcode',
'id' => 'postal_code'
),
// In case you want to limit your result like this example.
// Auto populate short name of `administrative_area_level_1`. For example: QLD
array(
'type' => 'select',
'name' => 'State',
'placeholder' => 'Select a State',
'options' => array(
'ACT' => 'ACT',
'QLD' => 'QLD',
'NSW' => 'NSW',
'NT' => 'NT',
'SA' => 'SA',
'TAS' => 'TAS',
'VIC' => 'VIC',
'WA' => 'WA'
),
'id' => 'administrative_area_level_1_short'
),
// You can set different ID but still can auto populate `route` to this field.
array(
'type' => 'text',
'name' => 'Route',
'id' => 'dummy_field',
'binding' => 'route'
),
// We have custom `geometry` address component. Which is `lat + ',' + lng`
array(
'type' => 'text',
'name' => 'Geometry',
'id' => 'geometry'
),
// But you can always use Binding Template to bind data like so
array(
'type' => 'text',
'name' => 'Geometry with custom template',
'id' => 'geometry2',
'binding' => 'lat + "," + lng' // lat,lng
),
// Here is the advanced usage of Binding Template.
// Put any address component + anything you want
array(
'type' => 'text',
'name' => 'State + Country',
'id' => 'state_country',
// Example Output: QLD Australia
'binding' => 'short:administrative_area_level_1 + " " + country'
),
)
);
return $meta_boxes;
} );
@arungpisyadi
Copy link

@tanftw I did, but the checking for the premium account was not on my hand. I've discussed with my employer about the condition and we decided to keep it as future plan for now because it seems we're gonna need to do some core programming modification on it and the feature is actually not the main feature of the project currently. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment