Last active
May 28, 2019 09:08
-
-
Save sabrysuleiman/3d8901b5ba6994a7a7fb3a4a5bb2e57e to your computer and use it in GitHub Desktop.
Wordpress Contact Form Function
This file contains hidden or 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 | |
################################################## | |
## Wordpress Contact Form Function | |
################################################## | |
function html_form_code() { | |
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">'; | |
echo '<p class="form-group">'; | |
echo '<input required aria-label="Your Name (required)" placeholder="Your Name (required)" type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />'; | |
echo '</p>'; | |
echo '<p class="form-group">'; | |
echo '<input required aria-label="Your Email (required)" placeholder="Your Email (required)" type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />'; | |
echo '</p>'; | |
echo '<p class="form-group">'; | |
echo '<input required aria-label="Contact Number (required)" placeholder="Contact Number (required)" type="text" name="cf-number" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />'; | |
echo '</p>'; | |
echo '<p class="form-group">'; | |
echo '<input required aria-label="Subject (required)" placeholder="Subject (required)" type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" size="40" />'; | |
echo '</p>'; | |
echo '<p class="form-group">'; | |
echo '<textarea required name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>'; | |
echo '</p>'; | |
echo '<p class="form-group"><input aria-label="Send" type="submit" name="cf-submitted" value="Send"/></p>'; | |
echo '</form>'; | |
} | |
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"] ); | |
$email = sanitize_email( $_POST["cf-email"] ); | |
$phone = sanitize_text_field( $_POST["cf-number"] ); | |
$subject = sanitize_text_field( $_POST["cf-subject"] ); | |
$message2 = esc_textarea( $_POST["cf-message"] ); | |
$message = "From: $name <$email>" . "\r\n" . "Phone: <$phone>" . "\r\n" . $message2 ; | |
// get the blog administrator's email address | |
$to = '[email protected]'; | |
$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 class="alert">'; | |
echo '<span class="closebtn" onclick="this.parentElement.style.display=\'none\';">×</span>'; | |
echo '<p>Thanks for contacting me, expect a response soon.</p>'; | |
echo '</div>'; | |
} else { | |
echo '<div class="alert dangerous">'; | |
echo '<span class="closebtn" onclick="this.parentElement.style.display=\'none\';">×</span>'; | |
echo 'An unexpected error occurred'; | |
echo '</div>'; | |
} | |
} | |
} | |
function cf_shortcode() { | |
ob_start(); | |
deliver_mail(); | |
html_form_code(); | |
return ob_get_clean(); | |
} | |
add_shortcode( 'contact_form', 'cf_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment