Last updated on October 18, 2023

WPML translate WP Bakery Page Builder elements

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Translate WP Bakery Page Builder elements with WPML.

To translate WP Bakery Page Builder elements 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 WP Bakery Page Builder elements.

Here’s an example of how you can use the wpml_translate_string function to translate a WP Bakery Page Builder element:

<?php
function wpsnippets_translate_wpb_element($content) {
    if (function_exists('wpml_translate_string')) {
        $translated_content = wpml_translate_string('wpb', $content);
        return $translated_content;
    }

    return $content;
}
?>

In the example above, the wpsnippets_translate_wpb_element function takes the content of a WP Bakery Page Builder element as a parameter. It checks if the wpml_translate_string function exists (to ensure WPML is active), and if it does, it translates the content using the wpml_translate_string function with the 'wpb' context. The translated content is then returned.

You can use this function to translate the content of any WP Bakery Page Builder element, such as text blocks, image galleries, or any other custom elements you have created using WP Bakery Page Builder.

Note: Make sure you have WPML and WP Bakery Page Builder plugins installed and activated before using this code snippet.

Examples

Example 1: Translating WP Bakery Page Builder elements using WPML

This example demonstrates how to translate WP Bakery Page Builder elements using WPML. The code example shows how to retrieve the translated content for a specific element.

<?php
$element_id = 123; // ID of the WP Bakery Page Builder element
$language = 'fr'; // Language code for the desired translation

// Get the translated content for the element
$translated_content = apply_filters( 'wpml_translate_element', '', $element_id, $language );

// Output the translated content
echo $translated_content;
?>

In this code example, we use the wpml_translate_element filter provided by WPML to retrieve the translated content for a specific WP Bakery Page Builder element. We pass the element ID and the language code as parameters to the filter. The translated content is then stored in the $translated_content variable and can be outputted as needed.

Example 2: Translating WP Bakery Page Builder elements programmatically

This example demonstrates how to programmatically translate WP Bakery Page Builder elements using WPML. The code example shows how to update the translated content for a specific element.

<?php
$element_id = 123; // ID of the WP Bakery Page Builder element
$language = 'fr'; // Language code for the desired translation
$translated_content = 'Translated content'; // New translated content

// Update the translated content for the element
do_action( 'wpml_translate_element', $element_id, $language, $translated_content );
?>

In this code example, we use the wpml_translate_element action provided by WPML to update the translated content for a specific WP Bakery Page Builder element. We pass the element ID, language code, and the new translated content as parameters to the action. This allows us to programmatically update the translation for the element.

Example 3: Translating WP Bakery Page Builder elements in a custom plugin

This example demonstrates how to translate WP Bakery Page Builder elements in a custom plugin using WPML. The code example shows how to add translation support for a custom WP Bakery Page Builder element.

<?php
function wpsnippets_translate_custom_element( $content, $element_id, $language ) {
    // Check if the element is a custom element
    if ( $element_id === 'custom_element' ) {
        // Get the translated content for the custom element
        $translated_content = apply_filters( 'wpml_translate_custom_element', '', $language );

        // Return the translated content
        return $translated_content;
    }

    // Return the original content for non-custom elements
    return $content;
}
add_filter( 'wpml_translate_element', 'wpsnippets_translate_custom_element', 10, 3 );
?>

In this code example, we define a custom function wpsnippets_translate_custom_element that hooks into the wpml_translate_element filter. Inside the function, we check if the element ID matches our custom element (custom_element). If it does, we retrieve the translated content for the custom element using the wpml_translate_custom_element filter. We then return the translated content. This allows us to add translation support for our custom WP Bakery Page Builder element in a custom plugin.

Last updated on October 18, 2023. Originally posted on November 8, 2023.

Leave a Reply