Once done validating a form with ajax, I use php within my functions.php to actually send the form.

$name = $_POST['fname'];
$lname = $_POST['lname'];
$subject_line = $_POST['subject'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$captcha = $_POST['captcha'];

if (!empty($name) && !empty($lname) && !empty($email) && !empty($phone) && !empty($subject_line) && !empty($message) ) {

    $to = '[email protected]';
    $from = $subject_line;
    $subject = $subject_line;
    $body1 = "First Name: " . $name .
        "\nLast name: " . $lname .
        "\nEmail: " . $email .
        "\nPhone Number: " . $phone .
        "\nMessage: " . $message;
    if (mail($to, $subject, $body1, 'From: ' . $email)) {
        print "Thank you, we will get back to you shortly.";

When I add in the php to the functions.php, I get the fatal error Call to undefined add_action() in the theme's functions.php. Any suggestions on what I'm missing?

  • 1
    are you calling functions.php directly or are you using the AJAX API?
    – Milo
    Commented Aug 26, 2013 at 0:08
  • Calling it directly.
    – ColoursB
    Commented Aug 26, 2013 at 0:16
  • 1
    Then fix that and use the API.
    – fuxia
    Commented Aug 26, 2013 at 0:18
  • This answer should help explain the problem with accessing functions.php directly.
    – s_ha_dum
    Commented Aug 26, 2013 at 0:50
  • Thank you guys, but I'm still unsure of how to implement the AJAX API. What do I type where?
    – ColoursB
    Commented Aug 26, 2013 at 1:16

1 Answer 1


register an ajax action for submitting email:

Note handle_form is the name of your action in this example

add_action('wp_ajax_handle_form','handle_form_submit_111605' );

also add this in order to make it work for non logged in users

add_action('wp_ajax_nopriv_handle_form','handle_form_submit_111605' );

do validation and mail sending job in the callback function handle_form_submit_111605

In functions.php

function handle_form_submit_111605() {


User simply jquey ajax to submit the form through ajax In you js file

<script type="text/jaascript">

var submitdata =  myForm.serialize();
  url: 'http://domain/wp-admin/admin-ajax.php?action=handle_form',
      //action parameter is the same one you used while registering the ajax
  type: 'POST',
  data: submitData,
  success: function(data, textStatus, xhr) {
         // ...

See more here

  • Hi Dipesh, I'm assuming that I should be adding both add_action() to functions.php, but how does all of that communicate with validating the form? My jQuery is as follows $(document).ready(function(){ $("#loading").hide(); $("#myform").validate({ debug: false, rules: { input: "required" }, messages: { input: "error message" }, submitHandler: function(form) { $("#loading").show(); $.post('<?php echo $our_url . '/functions.php'; ?>', $("#myform").serialize(), function(data) { $('#results').html(data); $("#loading").hide(); }); } }); });
    – ColoursB
    Commented Aug 27, 2013 at 2:52
  • Your code says you are making front-end validation, add_action has nothing to do with client side validation, but yes, you can do server-side validation with php in the callback function registered with your ajax action.Besides, the whole idea behind using ajax was to actually submit the form without complete page reload
    – Dipesh KC
    Commented Aug 27, 2013 at 9:49
  • Can you explain it to me like I'm 5? I don't understand what you're suggesting sorry
    – ColoursB
    Commented Aug 28, 2013 at 0:57
  • Sure, please tell me what things have you done so far. I guess your form is ready and you have already validated before submitting the form, right?These are front-end or client-side stuffs( because these code run on the users browser). Next you need to submit the form to the server so that the message or whatever reaches to its target receiver. and this is called serverside stuff( because this code runs on the server). You may again need to make sure you have enough data like email and message to prevent errors,so you might need to validate serverside as well.
    – Dipesh KC
    Commented Aug 28, 2013 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.