Я создаю плагин для моей контактной формы, используя ajax и добавляю shortcode wordpress. Я не понимаю, как это сделать, и он работает отлично, и читайте несколько форумов о admin- ajax.php, но не понимаете, как передавать данные в этот файл.
Это код:
<?php /* Plugin Name: Formulario de contacto Plugin URI: http://www.e-world.co Description: Formulario de contacto con ajax Version: 1.0 Author: Jorge Moreno Author URI: http://www.e-world.co license: GLP2 */ function the_action_function(){ $adminemail = "jorge.moreno@e-world.co"; if ($_GET['send'] == 'comments') { $_uname = $_POST['name']; $_uemail = $_POST['email']; $_website = $_POST['website']; $_comments = stripslashes($_POST['comment']); $email_check = ''; $return_arr = array(); if($_uname=="" || $_uemail=="" || $_comments=="") { $return_arr["frm_check"] = 'error'; $return_arr["msg"] = "Please fill in all required fields!"; } else if(filter_var($_uemail, FILTER_VALIDATE_EMAIL)) { $to = $adminemail; $from = $_uemail; $subject = "Renew Email: " .$_uname; $message = 'Name: ' . $_uname . '<br><br> Email: ' . $_uemail . '<br><br> website: ' . $_website . '<br><br> Comment: ' . $_comments; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "Content-Transfer-Encoding: 7bit\r\n"; $headers .= "From: " . $from . "\r\n"; @mail($to, $subject, $message, $headers); } else { $return_arr["frm_check"] = 'error'; $return_arr["msg"] = "Please enter a valid email address!"; } echo json_encode($return_arr); } } function createAjaxContactForm() { return ' <div class="form"> <form action="process.php" method="post" name="ContactForm" id="ContactForm" > <div class="form-group"> <input type="text" class="form-control" name="name" placeholder="Full Name *"> </div> <div class="form-group"> <input type="text" class="form-control" name="email" placeholder="Email *"> </div> <div class="form-group"> <input type="text" class="form-control" name="website" placeholder="Website"> </div> <div class="form-group"> <textarea rows="5" class="form-control" name="comment" placeholder="Your Message *" style="height:175px;"></textarea> </div> <div id="message_post"></div> <input class="btn btn-default" type="submit" value="ENVIAR" name="submitf" id="submitf"> </form> </div>'; } add_shortcode('AjaxContactForm', 'createAjaxContactForm'); ?>
и мой аякс:
jQuery(function(){ jQuery("#ContactForm").submit(function(){ jQuery("#submitf").value='Please wait...'; jQuery.post("password/wp-admin/admin-ajax.php", jQuery("#ContactForm").serialize(), function(data){ if(data.frm_check == 'error'){ jQuery("#message_post").html("<div class='errorMessage'>ERROR: " + data.msg + "!</div>"); document.ContactForm.submitf.value='Resend >>'; document.ContactForm.submitf.disabled=false; } else { jQuery("#message_post").html("<div class='successMessage'>Your message has been sent successfully!</div>"); jQuery("#submitf").value='Send >>'; } }, "json"); return false; }); });