To add custom supports for custom post types in WordPress, you will need to use the add_post_type_support()
function.
add_action('init', 'register_custom_post_type_support');
function register_custom_post_type_support() {
add_post_type_support('my_custom_post_type', 'my_custom_support');
}
After adding support for the custom feature, you need to define the metabox containing the longitude
and latitude
fields. You can use the add_meta_box()
function to add a metabox to your custom post type edit screen.
add_action('add_meta_boxes', 'add_custom_metabox');
function add_custom_metabox() {
add_meta_box(
'custom_geolocation_metabox',
'Geolocation',
'render_custom_geolocation_metabox',
'my_custom_post_type',
'normal',
'default'
);
}
function render_custom_geolocation_metabox($post) {
// Retrieve longitude and latitude values
$longitude = get_post_meta($post->ID, 'longitude', true);
$latitude = get_post_meta($post->ID, 'latitude', true);
?>
<label for="longitude">Longitude:</label>
<input type="text" id="longitude" name="longitude" value="<?php echo esc_attr($longitude); ?>"><br>
<label for="latitude">Latitude:</label>
<input type="text" id="latitude" name="latitude" value="<?php echo esc_attr($latitude); ?>">
<?php
}
add_action('save_post', 'save_custom_geolocation');
function save_custom_geolocation($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
// Save longitude and latitude values
if (isset($_POST['longitude'])) {
update_post_meta($post_id, 'longitude', sanitize_text_field($_POST['longitude']));
}
if (isset($_POST['latitude'])) {
update_post_meta($post_id, 'latitude', sanitize_text_field($_POST['latitude']));
}
}
In the code above, replace my_custom_post_type
with the name of your custom post type, and my_custom_support
with the name of the custom support feature you want to add.