0

I have a HTML form which inserts data into a custom table. The code for inserting the data is in a PHP file .

My question is where do I place the PHP file in the WordPress filesystem? I have tried placing the file in the root, does not work. Placed it in the wp-content/themes/mytheme folder. Does not work.

I get an error Page could not be found.

<form id="XXXForm" method="POST" action="/xxx_insert.php">

Any help is greatly appreciated.

David

5
  • It will work in your theme folder. are you trying to directly call it or are you including it from another php?
    – rudtek
    Apr 10, 2019 at 17:48
  • Directly calling it. So i should put it in the wp-content/themes/mytheme folder? Also do I call it as /xxx_insert.php or just xxx_insert.php? Apr 10, 2019 at 17:52
  • Call it like /wp-content/themes/mytheme/xxx_insert.php
    – mrben522
    Apr 10, 2019 at 18:07
  • Get a HTTP 500 error. Apr 10, 2019 at 18:21
  • Calling a PHP file in your theme or plugin directly is not the way to do this, and can be a major security hole. You don't need to create a dedicated PHP file to handle the form
    – Tom J Nowell
    Apr 10, 2019 at 18:22

2 Answers 2

2

You need to integrate your form and php form processor with Wordpress. One method to do this in your theme as follows.

Create a page template in your theme folder, as described here. And put your form markup in it.

Don't specify an action on your form, and add a hidden field to your form with arbitrary name and value. We will check against, and then look for that input to handle your form input, For example:

<?php 
       /* Template Name: My Form 
*/ 

?>

<form method="POST">
    <input type="hidden" name="my_hidden_field" value="xyz">
    ....
</form>

Now create a page in WP Admin and assign above created template. Load this page in browser to display your form.

Next put this code in functions.php of your theme

add_action( 'init', function() {
    if ( empty( $_POST['my_hidden_field'] ) ) {
        return;
    }

    // handle form submission
    include "xxx_insert.php"; // Replace xxx_insert.php with exact path to php file
}

Other methods to achieve the result is writing a plugin or shortcode.

I hope this may helps!

3
  • Guys thanks for replying. In my staging environment, putting the file in the root i.e. public_html/staging worked fine . All I did was call the file as /xxx_insert.php. The same does not work in production Any clue why?. Apr 10, 2019 at 18:20
  • It depends upon the setup of staging and production environment. You can try ` ABSPATH.'xxx_insert.php'` where ABSPATH refers to directory of your WordPress installation. This will work only if WP is loaded. Or specify the full path instead of relative path. Note: WordPress is a CMS, it's supposed to handle all the requests made to it, not individual files in a plugin or theme. Apr 10, 2019 at 18:42
  • SEE this Apr 10, 2019 at 18:46
0

You don't need a dedicated PHP file to put in your form tag. The code that generates the form should also handle it. That way action=""

For example, here is a page template with a basic form:

<?php
/**
 * Page Template: Test Form
 */

if ( empty( $_POST['submitted'] ) ) {
    // the form hasn't been submitted, so display it
    ?>
    <form method="POST" action="">
        <input type="hidden" name="submitted" value="yes"/>
        Name: <input type="test" name="name" value=""/>
        <input type="submit">
    </form>
    <?php
} else {
    // it's been submitted, process the form
    $name = $_POST['name'];
    ?>
    <p>You said <?php echo esc_html( $name ); ?></p>
    <?php
}

Using a dedicated PHP file to submit your forms too is a security problem, and comes with maintenance problems ( WP functions won't be available so you'd have to bootstrap WP and all the problems that come with it ). Putting PHP files in the root of your host to be used in a theme or plugin is also a bad idea

Your Answer

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

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