Skip to content

Instantly share code, notes, and snippets.

@itzikbenh
Last active January 9, 2020 20:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itzikbenh/57ecbe469d6a2c220b6c8b5f6408037b to your computer and use it in GitHub Desktop.
Save itzikbenh/57ecbe469d6a2c220b6c8b5f6408037b to your computer and use it in GitHub Desktop.
WordPress contact form using the API
(function($) {
$(".submit-contact-form").on("click", function(e) {
e.preventDefault();
$.ajax({
url: theme_data.site_url + 'wp-json/send-contact-form/v1/contact',
method: 'POST',
data: data
}).done(function(data){
}).fail(function(data){
});
})
})(jQuery);
<?php
//Register endpoint
function ath_endpoints()
{
register_rest_route('send-contact-form/v1', '/contact/', array(
'methods' => 'POST',
'callback' => 'send_contact_form'
));
}
add_action( 'rest_api_init', 'ath_endpoints' );
<?php
define("API_KEY", "apikey");
define("DOMAIN_NAME", "domain.com");
function mailgun_send( $to, $subject, $message )
{
$curl = curl_init();
curl_setopt( $curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt( $curl, CURLOPT_USERPWD, 'api:key-'.API_KEY );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$plain = strip_tags( nl2br( $message ) );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_URL, 'https://api.mailgun.net/v3/'.DOMAIN_NAME.'/messages' );
curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
'from' => '<mailgun@'.DOMAIN_NAME.'>',
'to' => $to,
'subject' => $subject,
'html' => $message,
'text' => $plain
) );
$response = json_decode( curl_exec( $curl ) );
$info = curl_getinfo( $curl );
if( $info['http_code'] != 200 )
{
die( "Email failed to send" );
}
curl_close( $curl );
return $response;
}
function send_contact_form( WP_REST_Request $request )
{
$form_location = $request['form_location'];
$full_name = sanitize_text_field( trim( $request['full_name'] ) );
$email = sanitize_email( trim( $request['email'] ) );
$body = sanitize_text_field( trim( $request['body'] ) );
$errors = [];
if( empty( $full_name ) )
{
$errors["full_name"] = "Name is required";
}
if ( empty( $email ) || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) )
{
$errors["email"] = "Valid Email is required";
}
if ( empty( $body ) )
{
$errors["body"] = "Message is required";
}
$message = "<h4>From: ".$full_name."</h4>";
$message .= "<h4>Email: ".$email."</h4>";
if( $form_location == "get-coverage-modal" || $form_location == "get-coverage-page" )
{
$subject = "Get Coverage";
$company_name = sanitize_text_field( trim( $request['company_name'] ) );
$interest = sanitize_text_field( trim( $request['interest'] ) );
$message .= "<h4>Company: ".$company_name."</h4>";
$message .= "<h4>Interest: ".$interest."</h4>";
}
if( $form_location == "contact-page" )
{
$subject = sanitize_text_field( trim( $request['subject'] ) );
}
if( $form_location == "company-page" )
{
$subject = "Wrong/missing information on a company";
$company_name = sanitize_text_field( trim( $request['company_name'] ) );
if ( empty( $company_name ) )
{
$errors["company_name"] = "Company name is required";
}
$message .= "<h4>Company: ".$company_name."</h4>";
}
$message .= "<p>".$body."</p>";
if( count( $errors ) > 0 )
{
return new WP_Error( 'contact_form_errors', $errors, array( 'status' => 422 ) );
}
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( "email@email.com", $subject, $message, $headers );
mailgun_send( "email@email.com", $subject, $message );
return "success";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment