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 thread is resolved. Here is a description of the problem and solution.

Problem:
Unable to use wp_update_term to propely update translated terms. Encountered the error, "The slug already exists"

Solution:
To fix this we need to find the term language and then switch to correct language then update.
Consider using this example:-

add_action('wp', function (){
    $term_id = 4;
    $taxonomy = 'subject';
    $current_lang = apply_filters( 'wpml_current_language', NULL );
    
    $term_language_code = apply_filters( 'wpml_element_language_code', null, ['element_id' => $term_id, 'element_type' => $taxonomy] );
    do_action( 'wpml_switch_language', $term_language_code );
    
    $updated = wp_update_term( $term_id, $taxonomy, array('name'=> 'hello' . time()) );
   
    do_action( 'wpml_switch_language', $current_lang );
    
    if ( ! is_wp_error( $updated ) ) { 
	    var_dump($updated); 
    } else {
	    echo $updated->get_error_message(); 
    }
});

This topic contains 13 replies, has 2 voices.

Last updated by efthimiosM 1 month, 1 week ago.

Assigned support staff: Raja Mohammed.

Author Posts
September 2, 2019 at 12:54 pm #4497181

efthimiosM

I AM REOPENING THIS TICKET BECAUSE I DIDN'T GET A CHANCE TO TRY WHAT ITAMAR WROTE OR EVEN REPLY BACK! PLEASE DON'T CLOSE THE TICKET WPML SUPPORT PEOPLE BEFORE IT IS RESOLVED OR AT LEASE LET ME REPLY BACK !!!!!!

I am trying to:
Update a term translation with wp_update_term (after I have successfully updated the term in the original language).

My code is :

$update = wp_update_term( $term->term_id, $term->taxonomy, array('name' => $updatedName) );
if ( ! is_wp_error( $update ) ) { 
    echo 'OK'; 
} else {
    echo $update->get_error_message(); 
}   

I expected to see:
The translated term name to be updated with the new value.

Instead, I got:
The slug “xxx-xxx-xxx” is already in use by another term.

September 2, 2019 at 2:01 pm #4497557

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The issue has been posted to second-tier for their expert advice. I will keep you posted once I have an update.

September 2, 2019 at 10:37 pm #4500143

efthimiosM

Ok I'm posting my findings here for your 2nd-tier support to see and please get back to me on this:

As you can read here about wp_update_term : https://developer.wordpress.org/reference/functions/wp_update_term/

If the ‘slug’ argument in $args is missing, then the ‘name’ in $args will be used. It should also be noted that if you set ‘slug’ and it isn’t unique then a WP_Error will be passed back. If you don’t pass any slug, then a unique one will be created for you.

So, here's what happens depending on if I pass a slug or not:

If I don't pass a slug at all OR I pass the current term's slug I get error "The slug “xxx-xxx-xxx” is already in use by another term"
If I pass an empty slug it creates a new slug from term name and taxonomy. I.e. turns term-name-el to term-name-taxonomy-name
If I successfully update the term with an empty slug (which results in the term-name-taxonomy-name slug) AND THEN try to update the term slug back to the original one then the term name REVERTS BACK to the original value before my initial update ('Old Name')!! Is this supposed to be normal behavior??? WPML code is doing something with wp_update_term..

And here's my simple code:

$originalSlug = $term->slug;
$updateTranslation = wp_update_term( 12457, 'my_taxonomy', array('name'=>'New Name', 'slug'=>'') );
if ( ! is_wp_error( $updateTranslation ) ) { 
   echo 'OK';	
} else {
   echo .$updateTranslation ->get_error_message(); 
}

$updateSlug = wp_update_term( 12457, 'my_taxonomy', array('slug'=>$originalSlug) );
if ( ! is_wp_error( $updateSlug ) ) { 
   echo 'Slug Updated';				
} else {
   echo $updateSlug->get_error_message(); 
}

Waiting for your input on this.

September 3, 2019 at 3:21 am #4500815

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The issue needs further investigation.

I would request the entire code snippet used to update the terms and also database dump of your site to test the issue on my local environment. You can use the Duplicator plugin to create a snapshot of the site database and share the package link with me http://wordpress.org/plugins/duplicator/

I have enabled private fields for you to securely share the details.

September 3, 2019 at 8:25 am #4501985

efthimiosM

Raja Mohammed I have an update regarding my tests. I wrote to you yesterday that :

"If I successfully update the term with an empty slug (which results in the term-name-taxonomy-name slug) AND THEN try to update the term slug back to the original one then the term name REVERTS BACK to the original value before my initial update ('Old Name')!! Is this supposed to be normal behavior??? WPML code is doing something with wp_update_term.."

It appears that this has been some weird cache problem and this morning the code worked - meaning that I can update the term with wp_update_term by passing the name new value in the array args and an empty slug so that a new unique slug is generated (in the form of term-name-taxonomy-name) AND I have to call wp_update_term again to change the slug to the original one. So I need to make 2 consecutive updates to achieve the desired result which is update a translated term name while keeping the current slug.

As you understand this is a workaround and the bug still persists where WPML prohibits wp_update_term to update a translated term retaining the same slug.
From what I understand, it's like WPML code creates an new translation with the new term name and assigns a new unique slug and then deletes the old one but until the old one is deleted you can't have the same slug.

September 3, 2019 at 8:27 am #4501989

efthimiosM

Raja Mohammed instead of sending you the database for troubleshooting I can try to set up a new site, locally, and check if the problem persists. In that case, it will definitely be a WPML issue and not related to our site/theme/installation.

September 3, 2019 at 9:31 am #4502797

efthimiosM

Raja Mohammed Ι confirm the same behavior on a brand new site with WPML installed (just 2 languages).

This is the code I run on a fresh installation of WordPress with the default theme. I have a custom taxonomy named "Subject" on a custom post type named "Object" which I both created with Toolset Types. I then installed WPML and translated the custom taxonomy term and then I tried to update it through code using wp_update_term :

$updated = wp_update_term( 4, 'subject', array('name'=>'test-2') );
if ( ! is_wp_error( $updated ) ) { 
	var_dump($updated); 
} else {
	echo $updated->get_error_message(); 
}		

The error message I get is : The slug “test” is already in use by another term.

September 3, 2019 at 11:00 am #4503745

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thanks for the detailed information.

Please supply the snapshot of the test site where you are able to reproduce the issue. we can investigate the issue further.

September 4, 2019 at 11:35 am #4510443

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I have tested the issue in detail by moving the code inside the theme functions.php file.
The issue occurs when the same slug used to update the term but this is not the case when WPML is deactivated. I have escalated this further with all necessary details.

September 4, 2019 at 11:51 am #4510575

efthimiosM

Thank you Raja Mohammed, so it is indeed an issue with WPML. Let me know what you come up with.

September 4, 2019 at 2:56 pm #4512361

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I got feedback from the second tier, The code needs to be run on language switch for it to properly update the appropriate terms.

I am pasting here the correct way to use the wp_update_terms code with WPML

wp_update_term internally calls get_term but since you are on the English page and trying to update the Greek term WPML returns an error of the same slug.

To fix this we need to find the term language and then switch to correct language then update.
Consider using this example:-

add_action('wp', function (){
    $term_id = 4;
    $taxonomy = 'subject';
    $current_lang = apply_filters( 'wpml_current_language', NULL );
    
    $term_language_code = apply_filters( 'wpml_element_language_code', null, ['element_id' => $term_id, 'element_type' => $taxonomy] );
    do_action( 'wpml_switch_language', $term_language_code );
    
    $updated = wp_update_term( $term_id, $taxonomy, array('name'=> 'hello' . time()) );
   
    do_action( 'wpml_switch_language', $current_lang );
    
    if ( ! is_wp_error( $updated ) ) { 
	    var_dump($updated); 
    } else {
	    echo $updated->get_error_message(); 
    }
});

I have tested this on my local site and it works like a charm.

September 5, 2019 at 1:46 pm #4519707

efthimiosM

Thanks Raja Mohammed, I tested it locally and it works but on my live site it doesn't. It still changes the slug and I need to run another update to get the slug back to the original one. I will check again, though.

Nevertheless, I think this should be documented somewhere.

September 6, 2019 at 6:55 am #4523515

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad that helped,

I will add this to the resolved summary once the ticket is resolved so that other clients may benefit from it, it serves as a alternative documentation.

September 9, 2019 at 7:31 am #4534061

efthimiosM

For the time being I found a workaround to my problem.