-
-
Save tanftw/b626e45546223df55a24 to your computer and use it in GitHub Desktop.
<?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; | |
} ); |
Hi @arungpisyadi, it has been 6 years since I wrote this extension, I no longer work with Metabox and forgot it. Sorry. Please ask in support forum so they can help you, I believe its possible and easy solution since the script it almost js and you can just pass the config via js variable.
Hi @tanftw , thank you for the response. I've tried to reach the support forum but it seems no longer active or I was the one who couldn't find it. I raised the question on SOF as well. I have tried to do the below with the code to re-init the Google Places script but it throws a "non-iterable Object" error on the console log for the controller value. If you could point me in the correct direction or send me the right URL of the support forum, it'd be an awesome help.
let geoData = $('script.data-geo');
let geoInfo = geoData.data('geo');
geoInfo.componentRestrictions.country = cval.toLowerCase();
// console.log(geoInfo);
$('script.data-geo').attr('data-geo', '');
$('script.data-geo').attr('data-geo', JSON.stringify(geoInfo));
$('.rwmb-meta-box').each(function() {
console.log(this);
$(this).removeData('controller');
controller = new Map(this);
console.log(controller);
controller.init();
$(this).data('controller', controller);
});
Hi @arungpisyadi, did you tried to ask here? Maybe they will help you faster.
I don't have source code since PHP and WordPress aren't my main work right now, but seems like you go to the right direction. You update the geo.componentRestrictions.country
property and re-init the main geo function and it should works, try to find the initial function and call it when your condition changes, something like:
$('#select-box').change((value) => {
geo.componentRestrictions.country = value;
geo.reload()
})
@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. 🙂
Hi @tanftw
Could you possibly know how to custom the extension to update restriction dynamically on the front-end using Javascript? For example, I have a dropdown that allows users to select the country and I need the "address" field to limit the returned address based on that country.