Dear,
first of all thank you for the feedback.
I finally got the plugin to work with my html/css contact form.
In fact, to have greater flexibility and customization possibilities, I preferred not to use plugins for creating the contact form.
To get it to work for sending emails I had to add this code to my function.php file:
function send_contact_email() {
if (isset($_POST['nome']) && isset($_POST['cognome']) && isset($_POST['email']) && isset($_POST['telefono']) && isset($_POST['messaggio'])) {
$nome = sanitize_text_field($_POST['nome']);
$cognome = sanitize_text_field($_POST['cognome']);
$email = sanitize_email($_POST['email']);
$telefono = sanitize_text_field($_POST['telefono']);
$messaggio = sanitize_textarea_field($_POST['messaggio']);
$to = ‘[email protected]’;
$subject = 'Nuova richiesta di contatto';
$message = "Hai ricevuto una nuova richiesta di contatto:\n\n";
$message .= "Nome: $nome\n";
$message .= "Cognome: $cognome\n";
$message .= "Email: $email\n";
$message .= "Telefono: $telefono\n";
$message .= "Messaggio:\n$messaggio\n";
$headers = array(
'From: ' . $nome . ' ' . $cognome . ' <' . $email . '>',
'Reply-To: ' . $email
);
$result = wp_mail($to, $subject, $message, $headers);
if ($result) {
$confirmation_message = 'Grazie per averci contattato! Ti risponderemo al più presto.';
} else {
echo 'Si è verificato un errore durante l\'invio della richiesta di contatto. Per favore, riprova più tardi.';
}
}
}
add_action('template_redirect', 'send_contact_email');
Only one problem remains. As you can see the php code expects two different messages, depending on whether the submission was successful or not.
I used two different ways to test both.
The one with simple “echo ” is not good, as it inserts the response in the wrong place of the web page.
Instead with the “$confirmation_message ” I should be able to insert the message anywhere in my html.
So I try by entering this code:
<?php
if (isset($_SESSION['confirmation_message'])) {
echo '<div class="confirmation-message">' . $_SESSION['confirmation_message'] . '</div>';
unset($_SESSION['confirmation_message']); // Rimuove il messaggio di conferma dalla variabile di sessione dopo averlo mostrato
}
?>
Unfortunately it doesn’t work.
Do you have any fix or solution to suggest?
Thanks again!