Created
March 21, 2013 12:56
-
-
Save gicolek/5212821 to your computer and use it in GitHub Desktop.
Sample Ajax Form WP
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 | |
/** | |
* Validate data and send mail. | |
* | |
* @see http://codex.wordpress.org/Function_Reference/wp_mail | |
* @return {int} Status of message: | |
* -2 => Invalid data | |
* -1 => Failed to send | |
* 1 => OK | |
*/ | |
function sendMail() { | |
echo 'jebaka'; | |
$response = array( | |
'status' => -2, | |
'errors' => [] | |
); | |
if ( !isset( $_POST['name'] ) || !isset( $_POST['email'] ) || !isset( $_POST['company'] ) || !isset( $_POST['message'] ) ) { | |
echo json_encode( $response ); | |
die(); | |
} | |
$name = esc_attr( $_POST['name'] ); | |
$email = sanitize_email( $_POST['email'] ); | |
$company = esc_attr( $_POST['company'] ); | |
$message = esc_textarea( $_POST['message'] ); | |
if ( !strlen( $name ) ) | |
$response['errors']['name'] = "C'mon, what's your name?"; | |
if ( !is_email( $email ) ) | |
$response['errors']['email'] = "Please, give us valid email."; | |
if ( !strlen( $company ) ) | |
$response['errors']['company'] = "Please, tell us what's your company name."; | |
if ( !strlen( $message ) ) | |
$response['errors']['message'] = "No message, huh?"; | |
if ( empty( $response['errors'] ) ) { | |
$to = get_bloginfo( 'admin_email' ); | |
$subject = 'Contact from ' . get_bloginfo( 'name' ); | |
$headers[] = "From: $name <$email>"; | |
if ( array_key_exists( 'img', $_REQUEST ) ) { | |
// decode the base64-encoded image received | |
// drop the first 22 characters from the string received, | |
// (having the substring "data:image/png;base64,") | |
$imgData = base64_decode( substr( $_REQUEST['img'], 22 ) ); | |
// Path where the image is going to be saved | |
$file = '/uploads/image.png'; | |
// delete previously uploaded image with the same path | |
if ( file_exists( $file ) ) | |
unlink( $file ); | |
// write $imgData into the file | |
$fp = fopen( $file, 'w' ); | |
fwrite( $fp, $imgData ); | |
fclose( $fp ); | |
} | |
$isSent = wp_mail( $to, $subject, $message, $headers, $file ); | |
$response['status'] = $isSent ? 1 : -1; | |
} | |
echo json_encode( $response ); | |
die(); | |
} | |
add_action( 'init', 'icc_ajax_setup' ); | |
/** | |
* Sets up ajax actions for form posting | |
* | |
* @hook init | |
*/ | |
function icc_ajax_setup() { | |
// handle ajax requests | |
add_action( 'wp_ajax_sendMail', 'sendMail' ); | |
add_action( 'wp_ajax_nopriv_sendMail', 'sendMail' ); | |
// not logged in | |
if ( isset( $_REQUEST['action'] ) ) { | |
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ); | |
} | |
// logged in | |
if ( isset( $_POST['action'] ) ) { | |
do_action( 'wp_ajax_' . $_POST['action'] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment