The code snippet below demonstrates how to translate Contact Form 7 forms using the WPML plugin in WordPress. This is useful when you have a multilingual website and want to display the form in different languages.
function wpsnippets_translate_contact_form( $form ) {
if ( function_exists( 'icl_object_id' ) ) {
$form_id = icl_object_id( $form->id(), 'wpcf7_contact_form', true, 'en' );
$translated_form = WPCF7_ContactForm::get_instance( $form_id );
if ( $translated_form ) {
$form = $translated_form;
}
}
return $form;
}
add_filter( 'wpcf7_contact_form', 'wpsnippets_translate_contact_form' );
This code snippet uses the wpcf7_contact_form
filter hook to modify the Contact Form 7 form object. It checks if the WPML plugin is active (function_exists( 'icl_object_id' )
) and then uses the icl_object_id
function to get the translated form ID based on the current language. If a translated form exists, it replaces the original form with the translated form.
To use this code snippet, simply add it to your theme’s functions.php
file or a custom plugin file. After adding the code, the Contact Form 7 forms on your website will be automatically translated based on the current language set in WPML.
Note: This code assumes that you have already set up WPML and translated the Contact Form 7 forms in the WPML translation interface.
Examples
Example 1: Translating Contact Form 7 using WPML
This example demonstrates how to translate a Contact Form 7 form using the WPML plugin.
function wpsnippets_translate_contact_form( $form ) {
if ( function_exists( 'icl_object_id' ) ) {
$form_id = icl_object_id( $form->id(), 'wpcf7_contact_form', true );
if ( $form_id ) {
$form->set_id( $form_id );
}
}
return $form;
}
add_filter( 'wpcf7_contact_form', 'wpsnippets_translate_contact_form' );
This code snippet uses the wpcf7_contact_form
filter to translate a Contact Form 7 form using WPML. It checks if the icl_object_id
function exists (provided by WPML) and if it does, it retrieves the translated form ID using the icl_object_id
function. If a translated form ID is found, it sets the form ID to the translated ID using the set_id
method of the $form
object. Finally, it returns the modified form.
Example 2: Translating Contact Form 7 form fields using WPML
This example demonstrates how to translate the labels and placeholders of Contact Form 7 form fields using WPML.
function wpsnippets_translate_form_fields( $tag ) {
if ( function_exists( 'icl_translate' ) ) {
$tag->name = icl_translate( 'Contact Form 7', 'cf7-field-' . $tag->name, $tag->name );
$tag->values = icl_translate( 'Contact Form 7', 'cf7-field-' . $tag->name . '-values', $tag->values );
$tag->labels = icl_translate( 'Contact Form 7', 'cf7-field-' . $tag->name . '-labels', $tag->labels );
$tag->placeholders = icl_translate( 'Contact Form 7', 'cf7-field-' . $tag->name . '-placeholders', $tag->placeholders );
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'wpsnippets_translate_form_fields' );
This code snippet uses the wpcf7_form_tag
filter to translate the labels and placeholders of Contact Form 7 form fields using WPML. It checks if the icl_translate
function exists (provided by WPML) and if it does, it translates the field name, values, labels, and placeholders using the icl_translate
function. The translations are based on the field name and are prefixed with ‘cf7-field-‘ to ensure uniqueness. Finally, it returns the modified $tag
object.
Example 3: Translating Contact Form 7 error messages using WPML
This example demonstrates how to translate the error messages of Contact Form 7 using WPML.
function wpsnippets_translate_error_messages( $messages ) {
if ( function_exists( 'icl_translate' ) ) {
$messages['invalid_email'] = icl_translate( 'Contact Form 7', 'cf7-error-invalid-email', $messages['invalid_email'] );
$messages['invalid_required'] = icl_translate( 'Contact Form 7', 'cf7-error-invalid-required', $messages['invalid_required'] );
$messages['spam'] = icl_translate( 'Contact Form 7', 'cf7-error-spam', $messages['spam'] );
// Add more error messages to translate here
}
return $messages;
}
add_filter( 'wpcf7_validation_error_messages', 'wpsnippets_translate_error_messages' );
This code snippet uses the wpcf7_validation_error_messages
filter to translate the error messages of Contact Form 7 using WPML. It checks if the icl_translate
function exists (provided by WPML) and if it does, it translates the error messages using the icl_translate
function. The translations are based on the error message keys and are prefixed with ‘cf7-error-‘ to ensure uniqueness. Finally, it returns the modified $messages
array.