1

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.

  • 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 Aug 7 '18 at 15:46
1

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

1

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)

0

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.com, 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.com/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.org/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.com/

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.org/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!

0

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.com/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.com/hello-world?abcde=abc

Also make your FORM action as:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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