0

Questions

  • Assuming that I wanted to put all the code in a custom page like page-contact.php how can I structure my code so I can receive email? the code works in other PHP projects, but getting an error in a WordPress environment
  • Why am I getting an error that the page cannot be found when all i need is the code from the page to handle the form?

Background

I did read another source by Sitepoint about about WordPress emails but this is a tiny project and I just need this to work according to how this project is setup.

In another PHP project I had the form action to be send according to what is written in _contactform.php

<form action="_contactform.php" method="post" name='submitform'>

it works fine there. But when I reused the code in a WordPress project I then got an error that the page did not exist as it seemed to try to direct the user to the url

http://example.com/contact/_contactform.php

So rather than have the action be the _contactform.php I think I can just put the code in the same page but how would I wrap it, etc?

The php file for the form starts with this

<?php
    /* Set e-mail recipient */
    $orderemail  = "example@example.com";

    $name = check_input($_POST['name']);
    $email    = check_input($_POST['email']);
    $address = check_input($_POST['address']);

    $message = "
        Hello!

        Your contact form has been submitted by:

        Name: $name
        E-mail: $email

        Address:
        $address

        End of message
    ";

    /* Send the message using mail() function */
    mail($orderemail, $subject, $message);
0
<form action="_contactform.php" method="post" name='submitform'>

Note the action is _contactform.php, a relative URL, not an absolute URL, therefore it will get appended on to the current page.

However, it would be more reliable to submit the form to the same page and leave the action blank, then run the code in WordPress.

This way, at the beginning of the page, you can check to see if the form has been submitted, then handle it. If none of the GET or POST options are present, display the contact form

5
  • How would the latter be done? – JGallardo Dec 8 '17 at 1:12
  • ok good tip with the relative URL. had to adjust to something like <form action="<?php echo get_home_url(); ?>/wp-content/themes/themename/_contactform.php" method="post" name='submitform'> – JGallardo Dec 8 '17 at 1:19
  • @JGallardo that's standard PHP, you use an if statement combined with the empty or isset functions, e.g. if ( !empty( $_POST['name'] ) ) { – Tom J Nowell Dec 8 '17 at 1:20
  • Also note that submitting a form or making a request directly to a PHP file inside a WordPress theme is super, super bad for security, and makes your code very easy to break and very fragile. WP should handle all requests – Tom J Nowell Dec 8 '17 at 1:21
  • I believe you but not sure what you are suggesting as an alternative – JGallardo Dec 8 '17 at 1:33
0

You should use WordPress actions to check if the form was submitted (using a hidden field, or submit button value), and then send the email.

You should be using wp_mail instead of mail as well. https://developer.wordpress.org/reference/functions/wp_mail/

And recommend using WordPress sanitation to keep your site secure and prevent malicious exploits of your code: https://codex.wordpress.org/Data_Validation

Add a hidden field to submit form:

<input type="hidden" name="my_limited_info_form" />

To check for the POST just use the wp action (form action can be empty, to use same page), and check for a hidden field from your submit form (place this in your child theme's functions.php file or Code Snippets plugin):

add_action( 'wp', 'smyles_send_email' );

function smyles_send_email() {

    // Only proceed if our form was submitted
    if ( ! array_key_exists( 'my_limited_info_form', $_POST ) ) {
        return;
    }

    $orderemail = "example@example.com";

    $name    = sanitize_text_field( $_POST['name'] );
    $email   = sanitize_email( $_POST['email'] );
    $address = sanitize_text_field( $_POST['address'] );

    // This was missing?? You provided very limited details so i can only assume this was a mistake
    $subject = '';

    $message = "
        Hello!

        Your contact form has been submitted by:

        Name: $name
        E-mail: $email

        Address:
        $address

        End of message
    ";

    wp_mail( $orderemail, $subject, $message );
}

Or chances are you have a name value set in your submit button, just check for that.

My recommendation though would be to use a plugin, that is free, like Caldera Forms, which allows you to build custom forms and handles all of that in the backend for you.

https://wordpress.org/plugins/caldera-forms/

That is my recommendation with the very limited information provided

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.