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
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
Share Improve this question asked Apr 10, 2019 at 17:39 David D'LimaDavid D'Lima 33 bronze badges 5 |2 Answers
Reset to default 2You 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!
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
/wp-content/themes/mytheme/xxx_insert.php
– mrben522 Commented Apr 10, 2019 at 18:07