Hi,
Could you tell me if this is plugin territory? (my previous topic it was vague. you can delete it)
a contact.php file added in footer.php with get_template_part() that contains:
<form id="contact" data-url="<?php echo admin_url( 'admin-ajax.php' ); ?>">
<textarea name="message" class="msg"></textarea>
<button>Send</button>
</form>
some js code in the main js file of the theme.
$("#contact").on("submit", function(e) {
e.preventDefault();
$.ajax({
url: $(this).attr("data-url");,
type: $(this).attr("method");,
data: {
action: "my_function",
message: $(this).find(".msg").val();
},
dataType: "json",
beforeSend: function() {
// things
},
success: function (data) {
if(data.res === "good") {
// things
} else {
// other things
}
},
error: function() {
// things
},
complete: function() {
// things
}
});
});
a function in functions.php file:
<?php
add_action('wp_ajax_nopriv_my_function', 'my_function');
add_action('wp_ajax_my_function', 'my_function');
function my_function() {
$msg = wp_strip_all_tags($_POST['msg']);
$res = array('status' => 'good', 'msg' => '');
if( empty($msg) ) {
$res['status'] = 'notgood';
$res['msg']['msg_msg'] = 'type something';
} else {
$res['msg']['msg_msg'] = 'good';
$message = 'Message:' . $msg;
}
$headers = array(
'from' => 'From: ' . get_bloginfo('name'),
'type' => 'Content-Type: text/html; charset=UTF-8'
);
$subject = get_bloginfo('name');
$to ='ex@am.ple';
if($res['status'] === 'good') {
wp_mail($to, $subject, $message, $headers);
}
echo json_encode($res);
die();
}
?>