Wordpress主页表单不工作


Wordpress Homepage Form not working

我正在尝试在我的网站上制作一个联系表单。

我使用的脚本已经为我工作在过去的一个php静态网站。

现在我正试图在我的wordpress主页上实现它,它似乎不起作用。

当我提交时,它会重定向到博客页面,或者重定向到"主页",但取而代之的是博客条目。

我应该在提交时得到echo验证,但它没有发生。

我试过action=", action="#", PHP_SELF,action="/", action="index", action="index.php",没有人工作。

这是我的代码:

<?php
$send = $_POST['send']
// Address to get the mails
$EmailTo = "sebastianferreira58@gmail.com";

// Form input fields
$name = $_POST['name']; 
$email = $_POST['email']; 
$subject = explode('|', $_POST['dropdown']);;
$message = $_POST['message']; 
if (isset($send)) {
    if (empty($email)) {
        $output = 'Hey, I need your email to reply';
    }
    elseif (empty($name)) {
        $output = 'Ups, you forgot your name';
    }
    else{
        mail("sebastianferreira58@gmail.com", $subject, $message, "From: $email'n");
        $output = 'Thanks, your submission has been sent';
    }
}
?>

我的HTML表单

<form method="post" action="">
                <p><?php echo $output; ?></p>
                <label for="name">Your beautiful name goes here</label>
                <input type="text" name="name" id="name">
                <label for="mail">I need your email to write you back, type it below</label>
                <input type="email" name="mail" id="mail">
                <label for="dropdown">Are you looking for help in something specific or just wanted to say HI?</label>
                <select name="dropdown" id="dropdown">
                    <option>Just wanted to say Hi!</option>
                    <option>I need help with my brand</option>
                    <option>I want to launch a website</option>
                    <option>Other</option>
                </select>
                <label for="else">Is there something else you want to tell me?</label>
                <textarea name="else" id="else">
                </textarea>
                <button class="btn btn-red" name="send" type="submit">SEND</button>
            </form>

尝试在表单操作中使用完全限定的url,即www.yourdomain.com/form-processor-script.php.

作为一个边注,你通常不应该有一个表单提交给self -这是糟糕的"表单",因为它允许用户使用浏览器的刷新按钮重新提交表单数据。它还将逻辑注入到表示层中。查看Post-Redirect-Get或PRG模式

在做了一些研究之后,我发现运行表单动作的最好方法是将表单html和处理变成一个WP插件。

你可以在下面的代码中看到

<?php
/*
    Plugin Name: Contact Form Plugin
    Plugin URI: http://ferrius.co
    Description: Simple non-bloated WordPress Contact Form
    Version: 1.0
    Author: Sebastian Ferreira
    Author URI: http://ferrius.co
*/

function html_form_code() {
    echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
    echo '<br>';
    echo '<label for="cf-name">Your beautiful name goes here</label>';
    echo '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '"/>';
    echo '<label for="cf-email">I need your email to write you back, type it below</label>';
    echo '<input type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '"/>';
    echo '<label for="cf-subject">Are you looking for help in something specific or just wanted to say HI?</label>';
    echo '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" />';
    echo '<label for="cf-message">Is there something else you want to tell me?</label>';
    echo '<textarea name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';
    echo '<button class="btn btn-red" type="submit" name="cf-submitted" value="SEND"/>SEND</button>';
    echo '</form>';
}
function deliver_mail() {
    // if the submit button is clicked, send the email
    if ( isset( $_POST['cf-submitted'] ) ) {
        // sanitize form values
        $name    = sanitize_text_field( $_POST["cf-name"] );
        $email   = sanitize_email( $_POST["cf-email"] );
        $subject = sanitize_text_field( $_POST["cf-subject"] );
        $message = esc_textarea( $_POST["cf-message"] );
        // get the blog administrator's email address
        $to = "sebastianferreira58@gmail.com";
        $headers = "From: $name <$email>" . "'r'n";
        // If email has been process for sending, display a success message
        if ( wp_mail( $to, $subject, $message, $headers ) ) {
            echo '<div>';
            echo '<p>Thanks for contacting me, expect a response soon.</p>';
            echo '</div>';
        } else {
            echo 'An unexpected error occurred';
        }
    }
}
function cf_shortcode() {
    ob_start();
    deliver_mail();
    html_form_code();
    return ob_get_clean();
}
add_shortcode( 'ferrius_contact_form', 'cf_shortcode' );
?>

希望对大家有用。