【发布时间】:2021-12-29 06:05:42
【问题描述】:
我想创建一个 wordpress 扩展程序来在我创建的网站上显示联系表格。我是 wordpress 的初学者,我找不到解决问题的方法。 表单未发送,我在 Deliver_mail() 函数中创建的错误消息始终显示,但我不明白为什么? 这是我的代码。 感谢您的回答!
<?php
// FORMULAIRE DE CONTACT HTML
// ##########################
function html_form_code() {
echo ('
<section class="section-form d-flex flex-column justify-content-center align-items-center">
<div class="container text-center mt-5">
<h2 class="fs-3">Une question, une demande de devis ?</h2>
<p>Remplissez le formulaire ci-dessous, nous vous recontacterons rapidement !</p>
</div>
<form action=" '. esc_url( $_SERVER['REQUEST_URI'] ) .'" method="post" class="container mt-3 p-4">
<div class="form-floating mb-3">
<input type="text" class="form-control" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . ' " size="40"/>
<label for="floatingInput">Nom</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" name="cf-firstname" value=" ' . ( isset( $_POST["cf-firstname"] ) ? esc_attr( $_POST["cf-firstname"] ) : '' ) . ' " size="40" >
<label for="floatingInput">Prénom</label>
</div>
<div class="form-floating mb-3">
<input type="email" class="form-control" name="cf-email" value=" ' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) .' " size="40" >
<label for="floatingInput">Email</label>
</div>
<div class="form-floating mb-3">
<input type="tel" class="form-control" name="cf-tel" value="' . ( isset( $_POST["cf-tel"] ) ? esc_attr( $_POST["cf-tel"] ) : '' ) . ' " size="40" >
<label for="floatingInput">Téléphone</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" name="cf-address" value="' . ( isset( $_POST["cf-address"] ) ? esc_attr( $_POST["cf-address"] ) : '' ) . '" size="250" >
<label for="floatingInput">Adresse</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" name="cf-postal" value=" ' . ( isset( $_POST["cf-postal"] ) ? esc_attr( $_POST["cf-postal"] ) : '' ) . '" size="10" >
<label for="floatingInput">Code postal</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control"name="cf-city" value="' . ( isset( $_POST["cf-city"] ) ? esc_attr( $_POST["cf-city"] ) : '' ) . '" size="250" >
<label for="floatingInput">Ville</label>
</div>
<div>
<p class="fs-5 mt-4">Votre demande concerne :</p>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="cf-help" value="' . ( isset( $_POST["cf-help"] ) ? esc_attr( $_POST["cf-help"] ) : '' ) . '"/>
<label class="form-check-label" for="flexCheckDefault">
Les Aides de l\'État
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="cf-contract" value="' . ( isset( $_POST["cf-contract"] ) ? esc_attr( $_POST["cf-contract"] ) : '' ) . '">
<label class="form-check-label" for="flexCheckChecked">
Nos Contrats d\'Entretien
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="cf-quote" value="' . ( isset( $_POST["cf-quote"] ) ? esc_attr( $_POST["cf-quote"] ) : '' ) . '"/>
<label class="form-check-label" for="flexCheckChecked">
Une Demande de Devis
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="cf-other" value="' . ( isset( $_POST["cf-other"] ) ? esc_attr( $_POST["cf-other"] ) : '' ) . '"/>
<label class="form-check-label mb-4" for="flexCheckChecked">
Autre
</label>
</div>
</div>
<div class="form-floating mb-3">
<textarea class="form-control"></textarea>
<label for="floatingInput">Votre message</label>
</div>
<button type="submit" class="btn button-primary mt-3" name="cf-submitted">Envoyer</button>
</form>
</section>') ;
}
// ENVOYER LES DONNEES A L ADMIN
// ##############################
function deliver_mail() {
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {
// sanitize form values
$name = sanitize_text_field( $_POST["cf-name"] );
$firstname = sanitize_text_field( $_POST["cf-firstname"] );
$email = sanitize_email( $_POST["cf-email"] );
$tel = sanitize_text_field( $_POST["cf-tel"] );
$address = sanitize_text_field( $_POST["cf-address"] );
$postal = sanitize_text_field( $_POST["cf-postal"] );
$city = sanitize_text_field( $_POST["cf-city"] );
$message = esc_textarea( $_POST["cf-message"] );
// get the blog administrator's email address
$to = get_option( 'admin_email' );
$headers = "From: $name <$email>" . "\r\n";
// If email has been process for sending, display a success message
if ( wp_mail( $to, $subject, $message, $headers ) ) {
echo '<div>';
echo '<p>Merci de votre envoi, nous vous recontacterons très vite !</p>';
echo '</div>';
} else {
echo 'Echec lors de la validation du formulaire';
}
}
}
// APPELLEE LORSQUE LE SHORTCODE EST ACTIF
// #######################################
function cf_shortcode() {
ob_start();
deliver_mail();
html_form_code();
return ob_get_clean();
}
add_shortcode( 'sitepoint_contact_form', 'cf_shortcode' );
【问题讨论】:
-
你在哪个文件中添加了这段代码?
-
我在wordpress plugins文件夹下创建了form-contact文件夹,插件激活,表单正确显示。
标签: php wordpress plugins contact-form