Last updated on October 18, 2023

WPML translate WPForms forms

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

Translate WPForms forms with WPML.

To translate WPForms forms 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 form labels and messages, into different languages.

Here’s an example of how you can use the wpml_translate_string function to translate a WPForms form:

function wpsnippets_translate_wpforms_form( $form ) {
    if ( function_exists( 'wpml_translate_string' ) ) {
        foreach ( $form['fields'] as &$field ) {
            if ( isset( $field['label'] ) ) {
                $field['label'] = wpml_translate_string( 'wpforms', 'Form Field Label', $field['label'] );
            }
            if ( isset( $field['description'] ) ) {
                $field['description'] = wpml_translate_string( 'wpforms', 'Form Field Description', $field['description'] );
            }
            // Translate any other field properties as needed
        }
    }
    return $form;
}
add_filter( 'wpforms_frontend_form_data', 'wpsnippets_translate_wpforms_form' );

In this example, we define a custom function wpsnippets_translate_wpforms_form that hooks into the wpforms_frontend_form_data filter. This filter allows us to modify the form data before it is rendered on the front-end.

Inside the function, we check if the wpml_translate_string function exists to ensure that WPML is active. Then, we loop through each field in the form and use the wpml_translate_string function to translate the field’s label and description.

You can add additional code to translate other properties of the form fields, such as placeholders or error messages, as needed.

Remember to replace 'Form Field Label' and 'Form Field Description' with the actual strings you want to translate. You can also use variables or concatenate strings to create dynamic translations.

This code snippet can be useful when you have a multilingual website powered by WPML and want to provide translated versions of your WPForms forms to your users.

Examples

Example 1: Translating WPForms forms using WPML

This example demonstrates how to translate WPForms forms using the WPML plugin. The code snippet below shows how to retrieve the form ID and translate its title and description.

function wpsnippets_translate_wpforms_form( $form_id ) {
    if ( function_exists( 'wpml_object_id' ) ) {
        $translated_form_id = wpml_object_id( $form_id, 'wpforms' );
        if ( $translated_form_id ) {
            $form = wpforms()->form->get( $translated_form_id );
            if ( $form ) {
                $form->post_title = apply_filters( 'wpml_translate_single_string', $form->post_title, 'wpforms', 'form_title_' . $translated_form_id );
                $form->post_content = apply_filters( 'wpml_translate_single_string', $form->post_content, 'wpforms', 'form_description_' . $translated_form_id );
            }
        }
    }
    return $form;
}

Explanation: The wpsnippets_translate_wpforms_form function checks if the WPML plugin is active and retrieves the translated form ID using the wpml_object_id function. If a translated form ID is found, the function retrieves the form object using the WPForms API and applies translation to the form title and description using the wpml_translate_single_string filter. The translated form object is then returned.

Example 2: Translating WPForms form fields using WPML

This example demonstrates how to translate WPForms form fields using the WPML plugin. The code snippet below shows how to retrieve the form fields and translate their labels.

function wpsnippets_translate_wpforms_form_fields( $form_id ) {
    if ( function_exists( 'wpml_object_id' ) ) {
        $translated_form_id = wpml_object_id( $form_id, 'wpforms' );
        if ( $translated_form_id ) {
            $form_fields = wpforms()->form->get_fields( $translated_form_id );
            foreach ( $form_fields as $field ) {
                $field->label = apply_filters( 'wpml_translate_single_string', $field->label, 'wpforms', 'field_label_' . $field->id );
            }
        }
    }
    return $form_fields;
}

Explanation: The wpsnippets_translate_wpforms_form_fields function checks if the WPML plugin is active and retrieves the translated form ID using the wpml_object_id function. If a translated form ID is found, the function retrieves the form fields using the WPForms API and applies translation to each field label using the wpml_translate_single_string filter. The translated form fields are then returned.

Example 3: Translating WPForms form notifications using WPML

This example demonstrates how to translate WPForms form notifications using the WPML plugin. The code snippet below shows how to retrieve the form notifications and translate their subject and message.

function wpsnippets_translate_wpforms_form_notifications( $form_id ) {
    if ( function_exists( 'wpml_object_id' ) ) {
        $translated_form_id = wpml_object_id( $form_id, 'wpforms' );
        if ( $translated_form_id ) {
            $form_notifications = wpforms()->form->get_notifications( $translated_form_id );
            foreach ( $form_notifications as $notification ) {
                $notification->subject = apply_filters( 'wpml_translate_single_string', $notification->subject, 'wpforms', 'notification_subject_' . $notification->id );
                $notification->message = apply_filters( 'wpml_translate_single_string', $notification->message, 'wpforms', 'notification_message_' . $notification->id );
            }
        }
    }
    return $form_notifications;
}

Explanation: The wpsnippets_translate_wpforms_form_notifications function checks if the WPML plugin is active and retrieves the translated form ID using the wpml_object_id function. If a translated form ID is found, the function retrieves the form notifications using the WPForms API and applies translation to each notification’s subject and message using the wpml_translate_single_string filter. The translated form notifications are then returned.

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

Leave a Reply