Skip to content

Instantly share code, notes, and snippets.

@sabrysuleiman
Last active May 28, 2019 09:08
Show Gist options
  • Save sabrysuleiman/3d8901b5ba6994a7a7fb3a4a5bb2e57e to your computer and use it in GitHub Desktop.
Save sabrysuleiman/3d8901b5ba6994a7a7fb3a4a5bb2e57e to your computer and use it in GitHub Desktop.
Wordpress Contact Form Function
<?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 = 'service@site.com';
$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\';">&times;</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\';">&times;</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