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.
/**
* 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) {
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.
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) {
// 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
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.