Created
October 3, 2013 19:42
-
-
Save hansspiess/6815875 to your computer and use it in GitHub Desktop.
Wordpress: Simple Contact Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Template Name: Kontakt | |
*/ | |
?> | |
<?php /* other template code goes here... */ ?> | |
<?php | |
/* response strings */ | |
$missing_fields = 'Bitte füllen Sie alle Felder aus.'; | |
$email_invalid = 'Ihre E-Mail-Adresse ist ungültig.'; | |
$trapped = 'Bitte diese Seite nicht per Script aufrufen.'; | |
$error = 'Die Nachricht konnte nicht gesendet werden.'; | |
$success = 'Danke! Die Nachricht wurde gesendet.'; | |
/* process post vars, leave honeypot raw */ | |
$submitted = sanitize_text_field( $_POST['scf_submitted'] ); | |
$name = sanitize_text_field( $_POST['scf_name'] ); | |
$email = sanitize_text_field( $_POST['scf_email'] ); | |
$message = sanitize_text_field( $_POST['scf_message'] ); | |
$honeypot = $_POST['scf_message2']; | |
/* wp_mail vars */ | |
$mailto = get_option( 'admin_email' ); | |
$subject = 'Nachricht von ' . get_bloginfo('name'); | |
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n"; | |
if ( $submitted ) { | |
if ( $honeypot != "" ) { | |
scf_response("alert-danger", $trapped); | |
} else { | |
if ( !$name || !$email || !$message ) { | |
$alert[] = $missing_fields; | |
} | |
if ( !is_email( $email ) && $email ) { | |
$alert[] = $email_invalid; | |
} | |
if ( empty( $alert ) ) { | |
if ( wp_mail( $mailto, $subject, strip_tags( $message ), $headers ) ) { | |
$alert[] = $success; | |
scf_response("alert-success", $alert); | |
unset( $submitted, $name, $email, $message, $honeypot ); | |
} else { | |
$alert[] = $error; | |
scf_response("alert-danger", $alert); | |
} | |
} else { | |
scf_response("alert-warning", $alert); | |
} | |
} | |
} | |
/* output alert html */ | |
function scf_response( $class, $alertArr ){ | |
$alertStr = implode( '<br>', $alertArr ); | |
echo '<div class="alert ' . $class .'">' . $alertStr . '</div>'; | |
} | |
?> | |
<form role="form" action="<?php the_permalink(); ?>" method="post"> | |
<div class="form-group"> | |
<label for="scf_name">Name</label> | |
<input | |
type="text" | |
id="scf_name" name="scf_name" | |
value="<?php echo $name; ?>" | |
class="form-control" | |
placeholder="Ihr Name"> | |
</div> | |
<div class="form-group"> | |
<label for="scf_email">E-Mail</label> | |
<input | |
type="text" | |
id="scf_email" name="scf_email" | |
value="<?php echo $email; ?>" | |
class="form-control" | |
placeholder="Ihre E-Mail-Adresse"> | |
</div> | |
<div class="form-group"> | |
<label for="scf_message">Nachricht</label> | |
<textarea | |
rows="4" | |
id="scf_message" name="scf_message" | |
class="form-control" | |
placeholder="Ihre Nachricht"><?php echo $message; ?></textarea> | |
</div> | |
<div class="form-group" style="display:none;"> | |
<label for="scf_message2">Nachricht2</label> | |
<input type="text" name="scf_message2" name="scf_message2"> | |
</div> | |
<input type="hidden" name="scf_submitted" value="1"> | |
<button type="submit" class="btn btn-primary">Nachricht senden</button> | |
</form> | |
<?php /* other template code goes here... */ ?> |
I'm not receiving any errors that I can see, but I'm also not receiving an e-mail at the admin e-mail when testing the form
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, hope things are going well. Would both the PHP logic and the html form need to be within the WP loop for this to work? http://codex.wordpress.org/Function_Reference/wp_mail