0

I'm a bit new to custom wordpress pages and I purchased a HTML theme that I successfully converted into a Wordpress site. The thing is, that there is a contact form that I would like to use and I've followed the codex instructions on using admin-post.php file but the form is not getting submitted.

Below is the form in my footer.php:

<form id="contact-form" method="POST" action="http://tvans.gr/wp-admin/admin-post.php">
    <input type="hidden" name="action" value="send_form"/>
    <input type="hidden" name="custom_nonce" value="<?php echo $custom_form_nonce ?>"/>

    <input class="form-control form-text" id="name" type="text" name="name" value="" placeholder="Please Enter Name" required />
    <input class="form-control form-text" id="email" type="email" name="email" value="" placeholder="Please Enter Email" required />
    <textarea class="form-control form-text" id="message" name="message" placeholder="Message" rows="4" required></textarea>
    <input type="submit" class="btn-1" id="submit" name="submit" value="SUBMIT">
    <input type="reset" class="btn-1 distab-cell-middle cancel" name="clear" value="RESET">
</form>

And here is my functions.php file:

function prefix_admin_send_form(){
    echo $_POST['name'];
    print($_POST['email']);
    exit;
}

add_action('admin_post_send_form', 'prefix_admin_send_form');

The data in functions.php is for testing purposes but still I'm not getting any response. Is it something I did wrong here or is is something that I'm missing? Thanks in advance for any help.

2 Answers 2

1

You can't use admin-post.php directly in your form, you need implement an ajax request.

https://codex.wordpress.org/AJAX_in_Plugins

To send your email have a function wp_mail https://developer.wordpress.org/reference/functions/wp_mail/

If you have problem to receive the POST params review this answer WordPress AJAX with Axios

-1

You are using admin_post action, so you have to use nopriv action also if user is not logged in. currenly you are not getting any response beacuse user need to authenticated admin post action. nopriv is used to handle AJAX requests on the front-end for unauthenticated users.

Footer.php :

<form id="contact-form" method="POST" action="<?php echo admin_url( 'admin-post.php' ); ?>">
    <input type="hidden" name="action" value="send_form"/>
    <input type="hidden" name="custom_nonce" value="<?php echo $custom_form_nonce ?>"/>

    <input class="form-control form-text" id="name" type="text" name="name" value="" placeholder="Please Enter Name" required />
    <input class="form-control form-text" id="email" type="email" name="email" value="" placeholder="Please Enter Email" required />
    <textarea class="form-control form-text" id="message" name="message" placeholder="Message" rows="4" required></textarea>
    <input type="submit" class="btn-1" id="submit" name="submit" value="SUBMIT">
    <input type="reset" class="btn-1 distab-cell-middle cancel" name="clear" value="RESET">
</form>

functions.php

function prefix_admin_send_form(){
    echo $_POST['name'];
    print($_POST['email']);
    exit;
}

add_action('admin_post_send_form', 'prefix_admin_send_form');
add_action('admin_post_nopriv_send_form', 'prefix_admin_send_form' ); // You nedd to this action
1
  • I have tested and it is working fine. can somebody tell me whats wrong with my answer and why down voted ? Commented Jan 16, 2020 at 11:30

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.