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, 1 month ago.

Assigned support staff: sarah.n.

Author Posts
April 3, 2013 at 10:13 am #116702

Hugo

Hi there,

Here's the situation:
- My client has a website with thousands of categories (custom taxonomy). It's like an ebay, without auction.
- It's available in 5 different languages
- The categories goes to the sixth level in hierarchy

- Adding a new category, nothing bad happens.
- Translating a category till the third level, nothing bad happens.
- Editing the categories in the default language at every levels, nothing bad happens.

- Translating the categories above third level, "504 Gateway Time-out".

What's the process behind that takes so long...?

Here's an example of the url:
hidden link

Help?

April 3, 2013 at 11:16 am #116730

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hello Hugo,

I tried to replicate the issue you report but I can't so I have to request a database dump to try to replicate the problem on my local install. I will email you shortly asking for it.

April 3, 2013 at 11:18 am #116731

Hugo

Ok Sarah.

April 3, 2013 at 11:24 am #116736

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Thank you! I received the db dump. I assume the custom taxonomy is registered by the theme so could you also send me a copy of your theme?

April 22, 2013 at 4:26 pm #121645

Hugo

Hi Sarah,

Sorry fot this delay, but replicate the WordPress installation on your side is not an option because of its complexity...

I've been thinking in a workaround. Is it possible to insert the categories via mysql and create the relations that WPML creates between items?

April 23, 2013 at 5:13 am #121734

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hello Hugo,

I've been thinking in a workaround. Is it possible to insert the categories via mysql and create the relations that WPML creates between items?

Yes, that would be possible. Before I provide any sort of code, just wanted to confirm, you plan to be inserting the translations this way, correct?

April 23, 2013 at 4:59 pm #121947

Hugo

Yes, but just for a custom taxonomy and in a very specific context. Basically there's a parent taxonomy that for some reason lost relationships and I can't create any sub-categories. That's why the 504 timeout happens.

April 24, 2013 at 10:57 am #122171

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hugo,

I have prepared some code below to get you started. I would suggest you test this first with one custom taxonomy to make sure it works as expected before you try it for more terms.

You'll need to add the following in your theme's functions.php file.

/**
 * Inserts terms
 *
 * @param (array) $terms the terms to insert key the default term ID and value the translated term name
 * @param (string) $parent the parent term name 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(){
	global $sitepress;
	
	// set the element type (tax_category | tax_post_tag | tax_custom) and language code
	$element_type ='tax_category';
	$language_code = 'de';
	
	// insert the terms
	// see the wpml_insert_terms function comments for the parameters 
	$terms_to_insert = array(55 => 'DE: Child category', 58 => 'DE: Another child category');
	$inserted_terms = wpml_insert_terms($terms_to_insert, 'DE: A category', 'category');
	
	if(!empty($inserted_terms)){
		global $sitepress;
		// loop
		foreach($inserted_terms 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());	
		}
		
		echo "Success!";
	} else {echo "oops! something went wrong!";}
}

//hook on admin_notices
add_action( 'admin_notices','wpml_set_term_lang_info' );

The variables you will need to edit based on your data are these:

$element_type ='tax_category';
$language_code = 'de';
	
// insert the terms
// see the wpml_insert_terms function comments for the parameters 
$terms_to_insert = array(55 => 'DE: Child category', 58 => 'DE: Another child category');
$inserted_terms = wpml_insert_terms($terms_to_insert, 'DE: A category', 'category');

When you refresh your admin dashboard the function will be called. Make sure you refresh the screen not more than once! If everything goes well you should see a "success" message where admin notices usually display.

I tried to comment out the code so you should be fine. Let me know if you have any questions.
Would be interested to know how you get on with this.

May 18, 2013 at 5:55 pm #128457

Hugo

Hi Sarah,

I'm back again 🙂 First of all, thank you for your help. I still need some assistance though...

Imagine this scenario. I have 4 active languages: en (main language), fr, it and pt-pt. I want to add a child category.

Question 1:
- Where do I insert the parent category ID?

Question 2:
- Can I insert at the same time all translations?

May 20, 2013 at 6:22 am #128562

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hello Hugo,

Do I understand this correct that the category terms are not created yet for the default language?

May 21, 2013 at 9:18 pm #129232

Hugo

Sarah,

Te default language has all categories created. What I need is to create of child categories.

May 22, 2013 at 7:12 am #129302

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hello Hugo,

Thank you for getting back to me.

You asked:

1 - "Where do I insert the parent category ID?"
Sorry I should have been more clear on this. Enter the parent category ID or name in place of "DE: A category" (second parameter of wpml_insert_terms()) in the code below:

$element_type ='tax_category';
$language_code = 'de';
     
// insert the terms
// see the wpml_insert_terms function comments for the parameters
$terms_to_insert = array(55 => 'DE: Child category', 58 => 'DE: Another child category');
$inserted_terms = wpml_insert_terms($terms_to_insert, 'DE: A category', 'category');

2 - "Can I insert at the same time all translations?"
Eventually yes, but I wanted that you test the code first on one category to make sure it works without issues before you attempt to insert all translations.

Once you are certain it works for one parent category you can adjust the code as follows to insert the child categories of other parent categories:

/**
 * 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(){
    global $sitepress;
     
    // set the element type (tax_category | tax_post_tag | tax_custom) and language code
    $element_type ='tax_category';
    $language_code = 'de';
     
    // insert the terms
    // multidimmensional array
    $terms_to_insert = array(
		'PARENT CATEGORY ID' => array(
				ID => 'CHILD CATEGORY NAME', 
		),
		'ANOTHER PARENT CATEGORY ID' => array(
				ID => 'CHILD CATEGORY NAME',
				ID => 'CHILD CATEGORY NAME' 				
		),
	);
	
	// note: this will hold a multidimmensional 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, 'category');
	}
    
    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()); 
			}
		}
         
        echo "Success!";
    } else {echo "oops! something went wrong!";}
}
 
//hook on admin_notices
add_action( 'admin_notices','wpml_set_term_lang_info' );

This is where you will enter your data:

// multidimmensional array
    $terms_to_insert = array(
		'PARENT CATEGORY ID' => array(
				ID => 'CHILD CATEGORY NAME', 
		),
		'ANOTHER PARENT CATEGORY ID' => array(
				ID => 'CHILD CATEGORY NAME',
				ID => 'CHILD CATEGORY NAME' 				
		),
	);

Let me know if any of the above is still unclear.

May 27, 2013 at 10:53 am #130595

Helen

Hi Sarah,

Great work with the code provided in this thread. With minor adjustments I am using it to import (500+) bilingual categories.

However, I noticed that the listing (wp-admin/edit-tags.php?taxonomy=category) is not updated properly. My imported categories will show up under 'All languages' and only move to their respective locale list (e.g. English) when I edit them and click update (even without changing any values in the edit screen).

I suspect the listing uses premade indices which are not updated correctly because my script does not call an update function, but I might be wrong.

Is there a way to force an update for the localized category listing?

Thanks in advance!

FYI:
I found similar issues in <a href="http://wpml.org/forums/topic/categories/">this thread</a> attributed to outdated WPML installations, but I'm working with the latest version (on WP 3.5.1).

May 28, 2013 at 2:15 pm #130961

Helen

Sorry for barging in again.
I managed to find an acceptable workaround for the problem with the Category listing I described earlier:

In my import procedure I first insert the default term (in 'nl') with wp_create_category(). Then I insert language info with your wpml_set_term_lang_info() function. In my case wpml_set_term_lang_info($terms, 'tax_category', 'en').

This will correctly insert the categories and list them under 'All languages' and 'English' in the Category listing.

However, the Dutch default categories would only show up in the 'All languages' list, and not show up anywhere else in wp-admin. They would be shown in the 'Dutch' list only after I updated them from the 'All languages' list.

The workaround I found is to set the default language code in the wp_icl_translations database table for the terms added:

$wpdb->query("UPDATE wp_icl_translations SET language_code = 'nl' WHERE language_code = ''");

Note that this query works for me, because I can safely assume that any term without a language_code is supposed to have 'nl' set as the language_code. I would advise to test this assumption in your own context and if needed use `element_id` in the WHERE clause.
Also, it would be nice if WPML would run this 'update', preferably whenever categories are added with wp_create_category().

I hope I have not interfered in solving the problem for the OP.

May 29, 2013 at 11:44 am #131220

Hugo

Hey Sarah,

It works. Partially 🙂
It only inserts one value each time it runs, even if I have multidimensional arrays. Example:

$terms_to_insert = array(
        '350' => array(
                ID => 'CHILD CAT 1',
                ID => 'CHILD CAT 2'
        )
);

Only inserts 'CHILD CAT 2'.

$terms_to_insert = array(
        '350' => array(
                ID => 'CHILD CAT 1',
                ID => 'CHILD CAT 2'
        ),
        '350' => array(
                ID => 'CHILD CAT 3'
        )
 );

Only inserts 'CHILD CAT 3'

My question is, how can I insert multiple categories at the same time for the same parent category?
And my biggest concern still is how can we add translations?

Thanks a lot for your support. Really appreciate it.