Summary
In the tags widget in the Post Edit page, I need to know a way to filter through:
- the tags suggestions
- the tag terms update when saving or updating the post
At least that is what I think I need.
My Case
I'm using qTranslate (v.2.5.32) to create a multilingual frontend as well as backend. My concern here is showing the translated taxonomy terms in the backend, specifically the post tags in the Post Edit page.
qTranslate handles taxonomy translations by using custom fields (metaboxes) in the taxonomy edit page, one for each language, which are saved to the options table database.
The plugin translates taxonomy terms in the frontend very well, but in the backend they don't translate. Only terms in the default language are displayed despite the current language being viewed.
My Hack So Far
Now I managed to create a hack so that the terms in the right language would be displayed by using the code below:
if (is_admin()) {
/**
* Fix translation for:
* - Taxonomy & custom taxonomy names in Post Manage page
* - List of tags already added to the post in Post
* Edit page (but have issues when saving)
*/
function mod_wp_get_object_terms_qtranslate($terms, $obj_id, $taxonomy, $args) {
// $taxonomy output seems to be wrapped
// in single quotes, thus remove them to
// make the output valid
$tax = str_replace("'", "", $taxonomy);
if ( !is_taxonomy_hierarchical($tax) ) {
$meta = get_option('qtranslate_term_name');
$lang = qtrans_getLanguage();
if ( !empty( $terms ) ) {
foreach ($terms as $term) {
$term->name = $meta[$term->name][$lang];
};
};
}
return $terms;
}
add_filter( 'wp_get_object_terms', 'mod_wp_get_object_terms_qtranslate', 0, 4 );
/**
* Fix translation for:
* - Taxonomy names in Taxonomy Manage page
* - 'Popular Tags' in Taxonomy (Tags) Manage page
* - Category filter dropdown menu in Post Manage page
* - Category list in Post Edit page
* - 'Most Used' tags list in Post Edit page
* (but have issues when saving)
*/
function mod_get_terms_qtranslate($terms, $taxonomy) {
$meta = get_option('qtranslate_term_name');
$lang = qtrans_getLanguage();
if ( !empty( $terms ) ) {
foreach ($terms as $term) {
if ($meta[$term->name][$lang]) {
$term->name = $meta[$term->name][$lang];
}
};
};
return $terms;
}
add_filter( 'get_terms', 'mod_get_terms_qtranslate', 0, 3 );
}
The Problem
The problem now is in the tags widget in the Post Edit page. Though I managed to translate the list of tags that are already added to the post and the ones in the 'Most Used' list, I am encountering the following issues:
- Unable to get the tags suggestions to show the translated version of the terms. Currently it only suggests terms if I typed in terms from the default language.
- When the post is saved/updated, translated tags from the added tags list (either newly added or the existing ones) and from the 'Most Used' list are treated as new tags by Wordpress. If I added terms in the default language, then they will be saved normally.
How do I fix the above issues?
What I Have Tried
In the first function from my hack above (ie. mod_wp_get_object_terms_qtranslate
) I added wp_update_term
inside the foreach
loop so that the term data itself is updated to reflect the current language being viewed. I would assume this would result in retrieving the correct language in the tag suggestion list and in updating the post.
foreach ($terms as $term) {
$term->name = $meta[$term->name][$lang];
wp_update_term($term->term_id, $taxonomy, array(
'name' => $meta[$term->name][$lang]
));
};
This didn't seem to work in that the term data was not updated.