This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 24 replies, has 4 voices.

Last updated by jodiL 5 years, 2 months ago.

Assigned support staff: sarah.n.

Author Posts
May 29, 2013 at 1:03 pm #131265

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hello Hugo,

I had a look at the code I gave you and I can't see where the problem could be. Is it possible please to have access to your installation to double check this?

You should have my email from a previous time but if you do not let me know and I will email you again.

@helen, Apologies for not seeing your comment when you originally left it. Glad to hear that the code served as a starting point for you 🙂

May 30, 2013 at 5:44 pm #131654

Hugo

I do have it 🙂

May 31, 2013 at 6:19 am #131705

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hugo,

This is to inform you that I have received your email with the access details. Thank you.

I will have a look inside and update you here as soon as I know more.

May 31, 2013 at 9:47 am #131787

Hugo

Thank you Sarah 🙂

May 31, 2013 at 9:49 am #131789

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hugo,

I believe you were populating the multidimensional array wrongly mainly because of my fault of not explaining things clearly enough. Sorry for that!

In the comment that I included above function wpml_insert_terms()

I have the following:

@param (array) $terms the terms to insert key the default term ID and value the translated term name

I should have indicated this more clearly in the example multidimensional array:

// multidimensional array
    $terms_to_insert = array(
        'TRANSLATED PARENT CATEGORY ID' => array(
                DEFAULT TERM ID => 'TRANSLATED CHILD CATEGORY NAME',
        ),
        'ANOTHER TRANSLATED PARENT CATEGORY ID' => array(
                DEFAULT TERM ID => 'TRANSLATED CHILD CATEGORY NAME',
                DEFAULT TERM ID => 'TRANSLATED CHILD CATEGORY NAME'             
        ),
    );

And by DEFAULT TERM ID I refer to the term in the default language.

I noticed that in your implementation of the code you are inserting categories in the original language but this is not what I understood you wanted to do.

I was under the impression that you wanted to insert child category translations of existing (default language) child categories. If this is still the case the the code I provided will work provided that the multidimensional array is build correctly.

If you would like me to run a test I will need the following:

1. The ID of the translated parent term (in the language we are inserting the new terms)

2. The child term ID in the default language (to make the association between translation and original - for each child therm we will be inserting - min 2)

3. The translated child term name (for each child therm we will be inserting - min 2)

May 31, 2013 at 3:44 pm #131919

Hugo

Hey Sarah,

I want to insert new terms and then translate that new terms. But I think I can go from this to a custom solutions. Here's my idea:

1 - Build an array with all new terms to be inserted in the default language
2 - Run the script that will loop (or create a recursive function) the array and call wpml_insert_terms();
3 - Since wpml_insert_terms() returns the new term ID, loop all installed languages and insert the translation of that term.

Does it sound good?

June 3, 2013 at 4:40 am #132239

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hey Hugo,

You wrote:
"I want to insert new terms and then translate that new terms."

OK well this changes things.

Have a look at the solution we offered this user: http://wpml.org/forums/topic/i-want-to-combine-posts/#post-131109

You'll need to modify it to fit your needs but there's a decent explanation on what what you need to connect the default terms to their translations after you have used wp_insert_term();

June 18, 2013 at 10:43 pm #137009

Hugo

Hey Sarah,

I've made it work! Here's the complete script:

 
<?php
// to run as as independent script
include_once('../../../../../wp-config.php');

/*
Template Name: WPML Mechanism
*/

/**
 * Inserts terms
 *
 * @param (array) $terms the terms to insert key the default term ID and value the translated term name
 * @param (int/string) $parent the parent term name or ID, Null or empty if the terms will not have a parent
 * @param (string) $taxonomy the taxonomy (category | post_tag | custom)
 *
 * @return associative array
 */
function wpml_insert_terms($terms, $parent, $taxonomy){
    $feedback = array();
    // do our terms have a parent?
    if(!empty($parent)){
        $parent_term = term_exists( $parent, $taxonomy );
        if ($parent_term !== 0 && $parent_term !== null) {
            $parent_term_id = $parent_term['term_id'];
        }
    }
    // insert new terms
    foreach($terms as $d_term_id => $t_term_name){
        // with parent
        if(!empty($parent_term_id)){
            $translated_term = wp_insert_term($t_term_name, $taxonomy, array('parent'=> $parent_term_id));
        }
        // without parent
        else{
            $translated_term = wp_insert_term($t_term_name, $taxonomy);
        }  
        // the default term_taxonomy_id (this is what we save in icl_translations)
        $default_term = term_exists( $d_term_id, $taxonomy );          
        //pass the result in the $feddback array for output
        $feedback[$default_term['term_taxonomy_id']] = $translated_term['term_taxonomy_id'];
    }  
    return $feedback;
}


/**
 * Sets language details on newly inserted translated terms
 *
 */
function wpml_set_term_lang_info($language, $parent_category_id, $default_term_id, $category_name){
    global $sitepress;
      
    // set the element type (tax_category | tax_post_tag | tax_custom) and language code
    $element_type ='tax_ad_categories';
    $language_code = $language;
    $terms_to_insert = array(
        $parent_category_id => array(
                $default_term_id => $category_name,
        )
    );
    
   // note: this will hold a multidimensional array again.
    $inserted_terms = array();
    // loop
    foreach($terms_to_insert as $parent_cat_ID => $child_cats)
    {
        $inserted_terms[] = wpml_insert_terms($child_cats, $parent_cat_ID, 'ad_categories');
        $new_id = $inserted_terms[0][''];
    }
     
    if(!empty($inserted_terms)){
        global $sitepress;
        // loop
        foreach($inserted_terms as $k => $v){
            foreach($v as $d_term => $t_term){
                // get the trid from the original
                $trid = $sitepress->get_element_trid($d_term, $element_type);
                  
                // associate the translated term to the original           
                $sitepress->set_element_language_details($t_term, $element_type, $trid, $language_code, $sitepress->get_default_language()); 
            }
        }
        return $new_id;
        
    } else {echo "oops! something went wrong!";}
}

// ---------------------------------------------------------------------------------------------------------------
// EDIT

// Brands (same to all languages)
$category_names = array(
    'Berg',
    'Bianchi',
    'Bottecchia',
    'BMX',
    'Caloi',
    'Cannomdale',
    'Carraro',
    'Conalgo',
    'De Rosa',
    'Diamondback',
    'Diamondback BMX',
    'Eddy Merckx',
    'Giant',
    'Kona',
    'Merida',
    'Specialized'
); 


// Languages
$languages = array( 'en', 'es', 'fr', 'it', 'pt-pt' ); 

// Parent ID ( en, es, fr, it, pt-pt ); 
$parent_ids = array('45361', '45362', '45363', '45364', '45365'); // Vehicles -> Bicycles -> Brand

if($_GET['action'] == 'insert')
{
    foreach ($category_names as $category)
    {
        $default_term_id = null; // english
        $inc = 0;

        foreach ($languages as $language)
        {
            $parent_id   = $parent_ids[$inc];

            if($default_term_id == null){
                // adding the default lang sets new term id
                $default_term_id = wpml_set_term_lang_info($language, $parent_id, null, $category);
            }else{
                wpml_set_term_lang_info($language, $parent_id, $default_term_id, $category);
            }

            $inc++;            
        }

        echo $category."<br/>";
    }        
}
?>

It worked perfectly!

Thank you so much for you support... Truly.

June 19, 2013 at 6:10 am #137029

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

This is wonderful! Glad it works 🙂

July 28, 2014 at 7:58 am #443035

jodiL

Hi,

Having the same issue, is there a permanent fix for it?