Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Summary

In the tags widget in the Post Edit page, I need to know a way to filter through:

  1. the tags suggestions
  2. the tag terms update when saving or updating the post

At least that is what I think I need.

enter image description here

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:

  1. 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.
  2. 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.

share|improve this question
add comment

1 Answer

When user starts typing something into tag input, JavaScript makes request to admin-ajax.php with action set to ajax-tag-search to receive list of suggestions (if any).

In that file that action is recognized as belonging to core and wp_ajax_tag_search() function is added to dynamically generated wp_ajax_ajax-tag-search hook, which fires almost immediately after that.

Looking at source of the function it is rather inflexible (no filters or anything), but since it echoes results (new line delimited list of suggestions) and dies - you don't even need to bother with it.

Just make your own version of function for tag suggestion logic and hook it at earlier priority (also dying at end so that native one is never reached).

share|improve this answer
    
Sounds promising. Could you provide some code maybe so I could have a better idea? A sample of how you would go about this problem? And how about problem no.2? Any tips for that one as well? Cheers. –  gmaggio May 5 '13 at 23:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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