To translate Divi Builder modules using WPML, you can use the wpml_translate_string
function provided by the WPML plugin. This function allows you to translate any string, including the content of Divi Builder modules.
Here’s an example of how you can use the wpml_translate_string
function to translate a Divi Builder module:
<?php
function wpsnippets_translate_divi_module( $module_content ) {
if ( function_exists( 'wpml_translate_string' ) ) {
$translated_content = wpml_translate_string( 'divi-builder', $module_content );
return $translated_content;
} else {
return $module_content;
}
}
In the example above, we define a custom function wpsnippets_translate_divi_module
that takes the content of a Divi Builder module as a parameter. Inside the function, we check if the wpml_translate_string
function exists (to ensure that WPML is active). If it does, we use the wpml_translate_string
function to translate the module content using the ‘divi-builder’ context. Finally, we return the translated content.
You can use this function in your theme or plugin to translate the content of Divi Builder modules. Simply pass the module content to the wpsnippets_translate_divi_module
function, and it will return the translated content if WPML is active, or the original content if WPML is not installed or active.
This code snippet can be useful in multilingual websites built with Divi and WPML, where you need to translate the content of Divi Builder modules. It allows you to easily integrate WPML translation functionality into your custom theme or plugin.
Examples
Example 1: Translating a Divi Builder module using WPML
This example demonstrates how to translate a Divi Builder module using WPML. The code snippet shows how to retrieve the module content in the current language and display it on the frontend.
<?php
function wpsnippets_translate_divi_module( $module_id ) {
if ( function_exists( 'icl_object_id' ) ) {
$translated_module_id = icl_object_id( $module_id, 'et_pb_layout', true );
if ( $translated_module_id ) {
$translated_module = et_pb_get_saved_builder_layout( $translated_module_id );
if ( $translated_module ) {
echo do_shortcode( $translated_module['content'] );
}
}
}
}
?>
Explanation: The wpsnippets_translate_divi_module
function checks if the WPML plugin is active (function_exists( 'icl_object_id' )
). If it is, it retrieves the translated module ID using the icl_object_id
function. Then, it fetches the translated module content using the et_pb_get_saved_builder_layout
function and displays it on the frontend using do_shortcode
.
Example 2: Translating multiple Divi Builder modules using WPML
This example demonstrates how to translate multiple Divi Builder modules using WPML. The code snippet shows how to loop through a list of module IDs and display the translated content for each module.
<?php
function wpsnippets_translate_multiple_divi_modules( $module_ids ) {
if ( function_exists( 'icl_object_id' ) ) {
foreach ( $module_ids as $module_id ) {
$translated_module_id = icl_object_id( $module_id, 'et_pb_layout', true );
if ( $translated_module_id ) {
$translated_module = et_pb_get_saved_builder_layout( $translated_module_id );
if ( $translated_module ) {
echo do_shortcode( $translated_module['content'] );
}
}
}
}
}
?>
Explanation: The wpsnippets_translate_multiple_divi_modules
function accepts an array of module IDs as a parameter. It loops through each module ID and performs the same translation process as in Example 1. The translated content for each module is then displayed on the frontend.
Example 3: Translating Divi Builder modules with custom language switcher
This example demonstrates how to translate Divi Builder modules using WPML and a custom language switcher. The code snippet shows how to retrieve the current language and pass it as a parameter to the translation function.
<?php
function wpsnippets_translate_divi_module_with_language_switcher( $module_id ) {
if ( function_exists( 'icl_object_id' ) ) {
$current_language = apply_filters( 'wpml_current_language', null );
$translated_module_id = icl_object_id( $module_id, 'et_pb_layout', true, $current_language );
if ( $translated_module_id ) {
$translated_module = et_pb_get_saved_builder_layout( $translated_module_id );
if ( $translated_module ) {
echo do_shortcode( $translated_module['content'] );
}
}
}
}
?>
Explanation: The wpsnippets_translate_divi_module_with_language_switcher
function retrieves the current language using the wpml_current_language
filter. It passes the current language as an additional parameter to the icl_object_id
function, ensuring that the translation corresponds to the selected language. The translated module content is then displayed on the frontend as before.