Some easy points on offer…
I wish to action a PHP script from a WordPress page but where is the best place to store the script? The script works perfectly fine using a non-WordPress environment in a flat HTML directory structure.
and I want to action it from the page using the following code:
<form class="contact_form" form action="phpmailersub.php" method="post" enctype="multipart/form-data" name="contact_form" onsubmit="return validateForm()">
What will then go into form action=”file” ?
At current the submit action is searching for a page http://current/route/to/contact-test/phpmailersub.php
but obviously, WordPress cannot find it.
Your help would be very much appreciated! If I can do anything to articulate my query or if you need any additional material I will try my best to help.
UPDATE
At current the PHP file (phpmailersub.php) and class.phpmailer.php sits in: http://damtech.it/demo/etap/wp-content/plugins/phpmailersub.php
and my current code looks like this:
<form class="contact_form" action="<?php bloginfo('http://damtech.it/demo/etap/wp-content/plugins/'); ?><p>/phpmailersub.php" enctype="multipart/form-data" method="post" name="contact_form" onsubmit="return validateForm()">
<table class="contact">
<tbody>
<tr>
<td class="third"><input type="text" name="cf_mercury" required="" /></td>
<td><span style="color: #3c4247;"> Name</span><span style="color: #3492d1;">*</span></td>
</tr>
<tr>
<td class="third"><input type="email" name="cf_jupiter" required="" />
<span class="form_hint">Correct format "name@url.com"</span></td>
<td> <span style="color: #3c4247;">E-mail</span><span style="color: #3492d1;">*</span></td>
</tr>
<tr>
<td colspan="2"><textarea name="cf_uranus" required="" rows="6"></textarea></td>
</tr>
</tbody>
</table>
<table class="contact">
<tbody>
<tr>
<td width="50%"><button class="submit" onclick="document.getElementById('contact_form').submit()" type="submit">Send message</button></td>
<td style="text-align: right;" width="50%"><span class="c-clear"><button class="reset" style="color: #3c4247; letter-spacing: -0.5px;" onclick="document.getElementById('contact_form').reset()" type="reset">reset</button></span></td>
</tr>
</tbody>
</table>
</form>
You are actually closer in your first attempt.
My recommendation would be to create a folder called “scripts” in the WP root and put your PHP script files in here as well as your attachment(s). You will then need to declare the full path in your action attribute as the script does not now share the same file path as in your “flat HTML file structure”.
Your action code will then be
action="http://damtech.it/demo/etap/scripts/phpmailersub.php"
Easy as that. No need to create a plugin at all (although this may be better practice). Don’t forget to change the details within phpmailersub.php to accommodate the changes made in terms of attachment paths (server path), page directs etc.
Do not call your PHP files directly. This will break, because in some setups, the plugin directory might be located on another domain, and your file would operate without the WordPress context now.
Use the current URL as form action URL (or
admin-post.php
), then test if the request is aPOST
request and handle the form submission. Then redirect back to the original page with a 303 status header.Assuming there is no contact form plugin which matches your needs, the proper way to do it in wordpress is to write and add an appropriate page template to your theme which contains and handles your form. Once done you associate the page in which the form should be displayed with the page template and then your action path is simply “”.