Skip to content Skip to sidebar

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client requested a custom script to bulk update the source language from German to English for all Elementor and WooCommerce posts using WPML.
Solution:
We advised against using custom scripts for modifying WPML settings due to potential risks and lack of support for such modifications. Instead, if WPML was recently installed and no translations have been made, we recommended performing a WPML reset and setting the correct default language from the beginning. Detailed instructions for this can be found here: WPML Reset Guide.
If translations already exist, the supported method is to manually disconnect and reconnect the translations while setting the correct default language. Step-by-step guides for this process are available here:

Please note that this solution might be outdated or not applicable to your specific case. We highly recommend checking related known issues at WPML Known Issues, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If this does not resolve your issue, please open a new support ticket.

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 2 replies, has 1 voice.

Last updated by Andrey 4 weeks ago.

Assisted by: Andrey.

Author Posts
December 15, 2025 at 11:52 am #17663931

danielP-54

I just received from you this snippet:

/**
* Custom function to bulk update the source language for specified post types
*
* Use a plugin such as Code Snippets or Toolset Types to run one time only
*
* @param string $source_from Language code to convert from
* @param string $source_to Language code to convert to
* @param array $content_types Array of post types to convert, defaults to ['post']
*
* Example: convert the source language of pages from German to English
* wpmlsupp_bulk_update_source_lang( 'de', 'en', ['page'] );
*/

function wpmlsupp_bulk_update_source_lang($source_from, $source_to, $content_types = ['post'])
{
if (isset($source_from) && isset($source_to) && is_array($content_types)) {

// switch to current source language and get posts in that language
$current_language = apply_filters('wpml_current_language', null);
do_action('wpml_switch_language', $source_from);
$contents = get_posts([
'numberposts' => -1,
'post_status' => 'any',
'post_type' => $content_types,
'suppress_filters' => false
]);

foreach ($contents as $content) {
// what's the trid for this content
$wpml_element_type = apply_filters( 'wpml_element_type', $content->post_type );
$trid = apply_filters( 'wpml_element_trid', null, $content->ID, $wpml_element_type );

// get all translations with this trid
$translations = apply_filters('wpml_get_element_translations', null, $trid, $wpml_element_type);

// iterate over translations with this trid and update source_language_code as required
foreach ($translations as $lang => $translation) {

$set_language_args = array(
'element_id' => $translation->element_id,
'element_type' => $translation->element_type,
'trid' => $trid,
'language_code' => $translation->language_code
);

if ( $lang == $source_to ) {
$set_language_args['source_language_code'] = "NULL";
} else {
$set_language_args['source_language_code'] = $source_to;
}
do_action( 'wpml_set_element_language_details', $set_language_args );
}
}
}
}

I asked to include all Elementor and WooCommerce posts to also change from de to en. Please give me the script, please tell me how to run the script once and to check whether it worked.

December 15, 2025 at 7:00 pm #17665612

danielP-54

I used this WPML generated script to set English as the default language and make sure the English pages are also seen as the original pages:

/**
* Custom function to bulk update the source language for specified post types
*
* Use a plugin such as Code Snippets or Toolset Types to run one time only
*
* @param string $source_from Language code to convert from
* @param string $source_to Language code to convert to
* @param array $content_types Array of post types to convert, defaults to ['post']
*
* Example: convert the source language of pages from German to English
* wpmlsupp_bulk_update_source_lang( 'de', 'en', ['page'] );
*/

function wpmlsupp_bulk_update_source_lang($source_from, $source_to, $content_types = ['post'])
{
if (isset($source_from) && isset($source_to) && is_array($content_types)) {

// switch to current source language and get posts in that language
$current_language = apply_filters('wpml_current_language', null);
do_action('wpml_switch_language', $source_from);
$contents = get_posts([
'numberposts' => -1,
'post_status' => 'any',
'post_type' => $content_types,
'suppress_filters' => false
]);

foreach ($contents as $content) {
// what's the trid for this content
$wpml_element_type = apply_filters( 'wpml_element_type', $content->post_type );
$trid = apply_filters( 'wpml_element_trid', null, $content->ID, $wpml_element_type );

// get all translations with this trid
$translations = apply_filters('wpml_get_element_translations', null, $trid, $wpml_element_type);

// iterate over translations with this trid and update source_language_code as required
foreach ($translations as $lang => $translation) {

$set_language_args = array(
'element_id' => $translation->element_id,
'element_type' => $translation->element_type,
'trid' => $trid,
'language_code' => $translation->language_code
);

if ( $lang == $source_to ) {
$set_language_args['source_language_code'] = "NULL";
} else {
$set_language_args['source_language_code'] = $source_to;
}
do_action( 'wpml_set_element_language_details', $set_language_args );
}
}
}
}

// Call the function to perform the bulk update
wpmlsupp_bulk_update_source_lang( 'de', 'en', ['post', 'page', 'product', 'elementor_library'] );

Then I went to the translation dashboard and tried to use ATE to translate a page from English into another language, for example German. When I went on and started translating this language, all strings were empty in the ATE. That should not be the case, since all translations have already been done.

How can I make sure I get the wanted end result:

- change default language from German to English
- set English as the original language per page instead of German
- have the existing German, Dutch and Danish languages set as translated page of the English page

Thanks for helping!

Regards, Daniel

December 15, 2025 at 8:15 pm #17665696

Andrey
WPML Supporter since 06/2013

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kyiv (GMT+02:00)

Hello Daniel,

Could you let me know where you got this script from? I wouldn’t recommend using custom scripts to make changes in WPML. The safest and supported approach is to use WPML’s own interface. We’re unable to debug any scripts or guarantee that they won’t cause issues or unintended side effects on your website.

If you’ve just installed WPML and haven’t created any translations yet, you can completely reset WPML and reconfigure it, making sure to choose the correct default language for your content from the start. You can find the instructions here:
https://wpml.org/documentation/getting-started-guide/language-setup/deleting-languages-and-plugin-data-by-doing-a-wpml-reset-on-your-site/#step-3-do-the-wpml-reset

If your content is already translated, the only supported way to change the default language of a post or page is to first disconnect the translations and then link them again, selecting the correct default language. Please refer to these guides for step-by-step instructions:
https://wpml.org/faq/how-to-disconnect-translations-from-the-default-language-page-or-post/
https://wpml.org/faq/how-to-link-already-translated-pages/
There is no bulk method for this, each post needs to be done manually.