0

I need to define a custom support feature for my WordPress Plugin but I can't find how to do so.

What I want to do is to display a box on certain custom post type containing 2 meta fields in which users can type in Longitude and Latitudes values for geolocalisation purposes.

For practical uses, i want to attach this custom feature the same way as supports are to posts :

<?php
$args = array(
    ...
    ...
    'supports' => array('title','editor', 'thumbnail', 'my_custom_support'),
    ...
    ...
);
register_post_type('my_custom_post_type', $args);
?>

OR, if my custom post type has already been created beforehand:

<?php
add_post_type_support('my_custom_post_type', 'my_custom_support');
?>

I know what I want but I just can't find how to register new supports ...

I hope someone can answer this problem because I'm actually tearing off my hairs!

1

2 Answers 2

0

Well you can add

...
'supports' => array('title','editor', 'thumbnail', 'custom-fields'),
...

You don't need to create anything beforehand, you can define the two fields latitude and longitude when adding a new post.

2
  • You can also create custom fields programatically, but it's somewhat complicated, and most people just use a plugin like ACF or Pods.
    – D. Dan
    Commented May 16, 2018 at 8:29
  • Thanks D. Dan for this answer but i don't think it solves my problem because afterwards i'll need to query every post type where my custom support appears. If i do it the way you suggested, it would be more difficult to do so because longitude and latitude would appear in every post-types where custom-fields support is set, whereas some custom post type shouldn't have those geolocalisation fields Actually you're right about ACF group fields, it's exactly what i want to have in my posts forms, but i don't want my plugin to depend on ACF
    – Psychotiik
    Commented May 16, 2018 at 8:49
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.