I built a custom HTML price calculator with a form that when gets submitted calls a PHP file through the action attribute and this PHP file then sends the collected data to my company’s email and another email to the customer who filled out the form.
Everything works on another website we built which is not a WordPress website, but I have no idea where to put this PHP file to make it work in WordPress, since I am new to WordPress. I already managed to put the calculator inside our theme, but I am stuck now.
We are currently using the BeTheme, but won’t necessarily stick with it, if it is easier to do this without it.
Any help will be appreciated.
I agree with other answers that most of the time it is better to use a plugin to handle form submissions. However, there are exceptions to this rule.
If you find yourself in a situation where you need to handle the form submissions yourself, you can submit data to admin-post.php
. Here is how to set that up:
First set up your form.
<!-- Submit the form to admin-post.php -->
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="POST">
<!-- Your form fields go here -->
<!-- Add a hidden form field with the name "action" and a unique value that you can use to handle the form submission -->
<input type="hidden" name="action" value="my_simple_form">
</form>
Now you will set up an action in your functions.php
file. You will use the value that you used in your hidden action field in the form.
function my_handle_form_submit() {
// This is where you will control your form after it is submitted, you have access to $_POST here.
}
// Use your hidden "action" field value when adding the actions
add_action( 'admin_post_nopriv_my_simple_form', 'my_handle_form_submit' );
add_action( 'admin_post_my_simple_form', 'my_handle_form_submit' );
Here is a link to more information from the codex – https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)