• Resolved cutu234

    (@cutu234)


    Thank you very much for your great plugin. I usually use the “essential_form_settings” filter for text. Now, I had to configure it on a multilingual site. Would you approve this approach? It seems to work perfectly fine. I just want to make sure that I’m not missing anything:

    add_filter('essential_form_settings', function($options) { // NOSONAR
        $email = 'kontakt@example.com';
        $lang = function_exists('pll_current_language')
            ? pll_current_language()
            : 'de';
        $translations = [
            'de' => [
                'email_from' =>  $email,
                'email_to' =>  $email,
                'email_subject' => 'Eine Nachricht aus dem Kontaktformular',
                'label_name' => 'Name',
                'label_email' => 'E-Mail',
                'label_message' => 'Nachricht',
                'button_text' => 'Abschicken',
                'agreement_text' => 'Ich stimme den Bestimmungen zum Datenschutz zu.',
                'success_message' => 'Die Nachricht wurde gesendet. Vielen Dank!',
                'name_missing_error' => 'Bitte das Pflichtfeld NAME ausfüllen!',
                'email_missing_error' => 'Bitte das Pflichtfeld EMAIL ausfüllen!',
                'email_not_valid_error' => 'Die E-Mail scheint nicht korrekt zu sein.',
                'message_missing_error' => 'Bitte das Pflichtfeld NACHRICHT ausfüllen!',
                'message_too_long_error' => 'Die Nachricht ist zu lang. Bitte nicht mehr als 50.000 Zeichen!',
                'missing_agreement_error' => 'Bitte den Bestimmungen zum Datenschutz zustimmen.'
            ],
            'en' => [
                'email_from' =>  $email,
                'email_to' =>  $email,
                'email_subject' => 'A message from the contact form',
                'label_name' => 'Name',
                'label_email' => 'Email',
                'label_message' => 'Message',
                'button_text' => 'Send',
                'agreement_text' => 'I agree to the privacy policy.',
                'success_message' => 'Your message has been sent. Thank you!',
                'name_missing_error' => 'Please fill in the required NAME field!',
                'email_missing_error' => 'Please fill in the required EMAIL field!',
                'email_not_valid_error' => 'The email address appears to be invalid.',
                'message_missing_error' => 'Please fill in the required MESSAGE field!',
                'message_too_long_error' => 'Your message is too long. Maximum 50,000 characters!',
                'missing_agreement_error' => 'Please agree to the privacy policy.'
            ],
            'fr' => [
                'email_from' =>  $email,
                'email_to' =>  $email,
                'email_subject' => 'Un message du formulaire de contact',
                'label_name' => 'Nom',
                'label_email' => 'E-mail',
                'label_message' => 'Message',
                'button_text' => 'Envoyer',
                'agreement_text' => 'J’accepte la politique de confidentialité.',
                'success_message' => 'Votre message a été envoyé. Merci !',
                'name_missing_error' => 'Veuillez remplir le champ NOM !',
                'email_missing_error' => 'Veuillez remplir le champ EMAIL !',
                'email_not_valid_error' => 'L’adresse e-mail semble invalide.',
                'message_missing_error' => 'Veuillez remplir le champ MESSAGE !',
                'message_too_long_error' => 'Votre message est trop long. Maximum 50 000 caractères !',
                'missing_agreement_error' => 'Veuillez accepter la politique de confidentialité.'
            ],
        ];
        return $translations[$lang] ?? $translations['de'];
    });
    
    add_filter('essential_form_agreement_text', function($text) { // NOSONAR
        $lang = function_exists('pll_current_language')
            ? pll_current_language()
            : 'de';
        $pages = [
            'de' => '/datenschutz/',
            'en' => '/en/privacy-policy/',
            'fr' => '/fr/protection-des-donnees/',
        ];
        $texts = [
            'de' => 'Ich stimme den Bedingungen zum <a href="%s">Datenschutz</a> zu.',
            'en' => 'I agree to the <a href="%s">privacy policy</a>.',
            'fr' => 'J’accepte la <a href="%s">politique de confidentialité</a>.',
        ];
        $url = get_home_url() . ($pages[$lang] ?? $pages['de']);
        return sprintf(
            $texts[$lang] ?? $texts['de'],
            esc_url($url)
        );
    });
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Jose Mortellaro

    (@giuse)

    Hi @cutu234

    thank you for sharing this very useful snippet.


    Regarding the essential_form_settings filter, I would replace

    return $translations[$lang] ?? $translations['de'];

    with

    $current_lang_settings = $translations[$lang] ?? $translations['de'];

    return array_merge($options, $current_lang_settings);

    I would do that because if in a future version of the plugin the $options array has new keys, with array_merge you will not lose them. As you probably know array_merge preserves all the keys that aren’t in your custom code, while overriding only the specific ones you’ve provided.
    With the current version of the plugin you don’t need array_merge, and probably you will never need it. This is just a caveat. But if the plugin’s core logic in the future introduces new keys to the $options array, your current code would overwrite everything and return an array with missing data.

    Let’s say that using array_merge makes your code more future-proof.

    I think the rest is perfect as it is. Thank you again for your snippet.

    Have a great day!

    Jose

    Thread Starter cutu234

    (@cutu234)

    Thank you very much for the input, Jose. Will change the snippet.

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.