admin管理员组

文章数量:1323529

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 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.

Share Improve this question edited Oct 8, 2018 at 22:24 Marc 7315 silver badges15 bronze badges asked Aug 7, 2018 at 15:19 Marina A.Marina A. 511 gold badge1 silver badge2 bronze badges 1
  • 1 Functionality really belongs in a plugin, not the theme. If you ever change the theme, you wouldn't want to lose your form/calculator. So, look into creating custom plugins - or, since there are tons of WP form plugins readily available, just use one of those. That is likely your best bet because they will be continually updated for security and compatibility with WordPress. If you really want your own custom HTML/CSS for the form, convert it to a custom plugin. – WebElaine Commented Aug 7, 2018 at 15:46
Add a comment  | 

4 Answers 4

Reset to default 4

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/Plugin_API/Action_Reference/admin_post_(action)

Best to put this kind of functionality in a plugin. Not cram it into the theme. You can see an example of a simple contact form plugin for wordpress here: https://www.sitepoint/build-your-own-wordpress-contact-form-plugin-in-5-minutes/

Tips:

  • Execute your code on the right moment, find the hook which suits you (ea. the init hook).
  • use wp_mail() in wordpress, not the standard php mail() function

As @WebElaine mentioned above: when you create a plugin for it you won't lose your functionality when switching themes. Also the functionality will be easy to transfer between sites if needed, and you can turn it on/off with the click of a button. If the functionality is only specific for this website, it's still a good idea no to include it in the (child theme's) functions.php. Only "theme" specific functionality (widgets, including specific styling/js files /etc) should be placed there. ea. You wouldn't include the contact form 7 code in your child theme

There is an easier way :)

Just add all your code as 'Form' + 'PHP part' in any of Your theme .php file. ANY! Then create the "If else" check under/with any PARAM you want. So the form will appear only, if that parameter will be within the main URL of your browser (current page).

For example: you have URL like example/hello-world (any post url)

just add in "single.php" something like:

if ( $_GET['abcde'] == 'abc') {
  ... your code ...
}

and now just load the page under the next URL: example/hello-world?abcde=abc

Also make your FORM action as:

<form action="">  // without any URL

There's a quick-and-dirty way of doing things: if this is a largely standalone WordPress and price calculator + emailer script, note that you can put an arbitrary PHP file in the root directory (or actually anywhere) on your site's file/dir tree, and unless you have rewrite rules and/or a server config to the contrary (note: you likely don't) then it can simply be accessed independently.

For example, if you have a test.php script and your WordPress site is available online at https://example, and you plop test.php in the root directory of your site (e.g. where you see files like wp-config.php and folders like wp-content/) then you and your site's visitors can access test.php from a browser at: https://example/test.php. Other forms served up, either by WordPress or completely independently from WordPress, could POST to test.php.

For a beginner-friendly WordPress way of setting up contact-type functionality, you could use one of the many contact form plugins out there. A popular option is Contact Form 7: https://en-ca.wordpress/plugins/contact-form-7/. There are dozens of similar options.

A great commercial forms option that can be used to create dynamic forms with conditional fields, etc is Gravity Forms: https://www.gravityforms/

It sounds like your script might be doing two things: there's a "price calculator" and there's a thing that "emails your company". If you need to integrate a calculator with your theme, you could try implementing it directly into your theme's template files, or perhaps creating it as a Widget: https://developer.wordpress/themes/functionality/widgets/. If your theme defines "widget areas" your widget could be placed anywhere within them easily from the wp-admin.

Hope this helps! Cheers!

本文标签: How to submit data from HTML form