Best pratice to make taxonomy terms translatable without changing slugs?

I’m not really up to date on WPML and how it handles translations, but my general understanding is that in most cases it simply creates duplicate entries for each language and links them together with the “original”.

However, I recently hade a similar problem where I needed the ability to add a “pluralized” version of the term names (as opposed to the default field where I types the name in singular). I solved this by hooking onto <taxonomy>_add_form_fields, <taxonomy>_edit_form_fields, edited_<taxonomy> and create_<taxonomy>.

Solution #1 – add custom taxonomy field

define('TERM_PLURAL_NAME_KEY', 'term_name_plural');

function setup_taxonomies() {
    $taxonomy_name="my_tax";

    add_action( $taxonomy_name . '_add_form_fields', 'my_tax_add_plural_name_field', 10, 2 );
    add_action( $taxonomy_name . '_edit_form_fields', 'my_tax_edit_plural_name_field', 10, 2 );

    add_action( 'edited_' . $taxonomy_name, 'save_my_tax_meta', 10, 2 );
    add_action( 'create_' . $taxonomy_name, 'save_my_tax_meta', 10, 2 );
}
add_action('init', 'setup_plural_taxonomies')


function my_tax_add_plural_name_field() {
    ?>
        <div class="form-field">
            <label for="term_meta[<?php echo TERM_PLURAL_NAME_KEY; ?>]"><?php _e( 'Plural name', 'i18n_key' ); ?></label>
            <input type="text" name="term_meta[<?php echo TERM_PLURAL_NAME_KEY; ?>]" id="term_meta[<?php echo TERM_PLURAL_NAME_KEY; ?>]" value="">
            <p class="description"><?php _e( 'A pluralized version of the term title, to be used when multiple objects are referred to.', 'i18n_key' ); ?></p>
        </div>
    <?php
}

function my_tax_edit_plural_name_field($term) {
        $t_id = $term->term_id;
        $term_meta = get_option( "taxonomy_$t_id" ); ?>
        <tr class="form-field">
        <th scope="row" valign="top"><label for="term_meta[<?php echo TERM_PLURAL_NAME_KEY; ?>]"><?php _e( 'Plural name', 'i18n_key' ); ?></label></th>
            <td>
                <input type="text" name="term_meta[<?php echo TERM_PLURAL_NAME_KEY; ?>]" id="term_meta[<?php echo TERM_PLURAL_NAME_KEY; ?>]" value="<?php echo esc_attr( $term_meta[TERM_PLURAL_NAME_KEY] ) ? esc_attr( $term_meta[TERM_PLURAL_NAME_KEY] ) : ''; ?>">
                <p class="description"><?php _e( 'A pluralized version of the term title, to be used when multiple objects are referred to.', 'i18n_key' ); ?></p>
            </td>
        </tr>
    <?php
}

function save_my_tax_meta($term_id) {
    if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['term_meta'][$key] ) ) {
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        update_option( "taxonomy_$t_id", $term_meta );
    }
}

The above code could easily be modified to hold a localized string instead of the name in plural. You could then loop through the list of activated languages in WPML, and a a field for each language (you’d need to adjust all the functions above). This might be impractical though if you have a lot of languages enabled in WPML and might very well clutter the editor.

Which is why I could also imagine an alternate implementation…

Solution #2 – Connect “localized” terms with “default language” term

Not sure if WPML would do this automatically for you, but in case it does not you could let it create localized terms for each language, and write a function that, given a term, returns the localized language instance of the that term. You could then create this relation using the same hooks as in the example above, but instead adding two fields – the first holding the default language version of the term (using wp_dropdown_categories) and the other being holding the language of the localized term.

As I said, I have little experience with WPML and one could imaging this type of functionality is already implemented in the plugin itself, so I’d suggest investigating that before going down this path.

Leave a Comment