1

I'm trying to create a Wordpress shortcode that will output a full-width, filterable Google Map with a set of markers that will be loaded on first load (and shown or hidden depending on the filters chosen).

The thing is, I've never done / used a custom taxonomy before, and I thought that it would be a good idea to create one, I was thinking about "a class" called Marker with the attributes "latitude, longitude, title and description", but I am not sure on how to proceed in order to achieve this.

Can someone please help me on how to create this taxonomy, and then load all of it's items in Wordpress programatically? Thanks in advance.

  • Have you checked the WP documentation? codex.wordpress.org/Taxonomies – jdm2112 Sep 27 '16 at 15:40
  • Can I ask why you want to use taxonomies for this? – Ahmed Fouad Sep 27 '16 at 15:41
  • Well, I want to store these markers in some sort of system, that is easy for the users to mantain and update. Take into account that the final users of this system will have 0 experience in programming, and they would prefer to use Wordpress's interface (they have basic experience adding Posts and stuff, for example). – Oriol A. Sep 27 '16 at 15:42
  • @jdm2112 Yes, I have, and there is something missing. Like I said, it's the first time that I'm using this, so I'm not familiar with the procedure. And I am not 100% sure that I got the concept right. What I want to use, as far as I know, is some sort of combination between a custon taxonomy (let's call it "Markers"), and "custom fields", that will be "latitude, longitude", and so on". and I'm kinda lost on how to proceed. – Oriol A. Sep 27 '16 at 15:47
  • Understood. I think you may need to create taxonomy first see : codex.wordpress.org/Function_Reference/register_taxonomy then you'll need to add custom meta fields to that taxonomy see this: pippinsplugins.com/adding-custom-meta-fields-to-taxonomies – Ahmed Fouad Sep 27 '16 at 15:48
0

Here's a fully working example to get you started.

Register the "marker" taxonomy

register_taxonomy( 'marker',
    'post',
    array(
        'hierarchical'          => false,
        'update_count_callback' => '_update_post_term_count',
        'label'                 => __( 'Markers', 'textdomain' ),
        'labels' => array(
                'name'              => __( 'Markers', 'textdomain' ),
                'singular_name'     => __( 'Marker', 'textdomain' ),
                'menu_name'         => _x( 'Markers', 'Admin menu name', 'textdomain' ),
                'search_items'      => __( 'Search Markers', 'textdomain' ),
                'all_items'         => __( 'All Markers', 'textdomain' ),
                'parent_item'       => __( 'Parent Marker', 'textdomain' ),
                'parent_item_colon' => __( 'Parent Marker:', 'textdomain' ),
                'edit_item'         => __( 'Edit Marker', 'textdomain' ),
                'update_item'       => __( 'Update Marker', 'textdomain' ),
                'add_new_item'      => __( 'Add New Marker', 'textdomain' ),
                'new_item_name'     => __( 'New Marker Name', 'textdomain' )
            ),
        'rewrite'               => false,
    )
);

Now we need to add the hooks to add/save the fields we need for this taxonomy

add_action( 'marker_add_form_fields',   'marker_add_fields', 10, 2 );
add_action( 'marker_edit_form_fields',  'marker_edit_fields', 10, 2 );
add_action( 'created_marker',           'save_marker_meta', 10, 2 );
add_action( 'edited_marker',            'save_marker_meta', 10, 2);

This function adds the fields to add new taxonomy screen

function marker_add_fields() {
    ?>
    <div class="form-field">
        <label for="latitude"><?php _e( 'Latitude' ); ?></label>
        <input type="text" name="latitude" id="latitude" value="">
    </div>

    <div class="form-field">
        <label for="longitude"><?php _e( 'Longitude' ); ?></label>
        <input type="text" name="longitude" id="longitude" value="">
    </div>
    <?php
}

This function add the fields to the edit screen of taxonomy

function marker_edit_fields( $term, $taxonomy ) {
    $latitude   = get_term_meta( $term->term_id, 'latitude', true );
    $longitude  = get_term_meta( $term->term_id, 'longitude', true );
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="latitude"><?php _e( 'Latitude' ); ?></label></th>
        <td>
            <input type="text" name="latitude" id="latitude" value="<?php echo $latitude; ?>">
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="longitude"><?php _e( 'Longitude' ); ?></label></th>
        <td>
            <input type="text" name="longitude" id="longitude" value="<?php echo $longitude; ?>">
        </td>
    </tr>
    <?php
}

Finally, this function saves the custom field values for our new taxonomy

function save_marker_meta( $term_id, $tt_id ){
    if( isset( $_POST['latitude'] ) && '' !== $_POST['latitude'] ){
        update_term_meta( $term_id, 'latitude', $_POST['latitude'] );
    }
    if( isset( $_POST['longitude'] ) && '' !== $_POST['longitude'] ){
        update_term_meta( $term_id, 'longitude', $_POST['longitude'] );
    }
}
  • God bless you, really. Upvoted and marked as solved. THANKS A LOT! :D – Oriol A. Sep 28 '16 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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