尝试在wordpress中使用php创建联系表单

时间:2012-02-16 23:46:54

标签: php forms wordpress contact

我已编写代码以在php中提交表单和电子邮件,并将其放在我的wordpress自定义页面中。我已经包含了php发送电子邮件,但由于某种原因没有发送任何内容。我不知道从哪里开始调试这个东西。

以下是自定义页面的代码

<?php
/*
Template Name: page-pickup.php
*/
?>
<?php include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); ?>
<?  include_once(ABSPATH. 'wp-content/themes/fiftyfityNew/contact-form.php'); ?>

<?php add_post_meta(7, 'fruit', 'banana', true) or update_post_meta(7, 'fruit', 'banana'); ?>
<?php
get_header(); ?>
<?php
echo do_shortcode( '[contact-form-7 id="23" title="Contact form 1"]' );
?>

<?php
    if(isset($GLOBALS["mail_message"])) {
        echo "<p>".$GLOBALS["mail_message"]."</p>";
    }

    if(isset($_SESSION["mail_message"])) {
        echo "<p>".$_SESSION["mail_message"]."</p>";
        unset($_SESSION["mail_message"]);
    }
?>

<form method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<input type="hidden" name="form-name" value="contact" />
<div style="text-align:left; border: 1px solid #bbb; padding:10px;">
    <div>
        <label for="name">name</label>
        <input type="text" name="name" id="name" />
    </div>
    <div>
        <label for="email">email</label>
        <input type="text" name="email" id="email" />
    </div>
    <div>
        <label for="message">message</label>
        <textarea name="message" id="message" style="width:250px; height:100px"></textarea>
    </div>
    <input type="submit" value="go" />
</div>
</form>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

以下是我所包含的contact-form.php

的代码
<?
function send_message()
{
    $subject = "Test email form WordPress";
    $body = $_POST['message'];
    $recipient = "anderskitson@gmail.com";

    $success = @wp_mail($recipient, $subject, $body);

    if($success) {
        session_start();
        $_SESSION["mail_message"] = "Thank you. your message has been sent.";
        header( 'Location:  '.$_SERVER['HTTP_REFERER'] ) ;
    }
    else {
        $GLOBALS["mail_message"] = "Euston, we have a problem.";
    }
}
?>

1 个答案:

答案 0 :(得分:2)

为什么使用CF7并同时创建自己的表单? CF7为您完成所有处理。