Wordpress自定义联系表单不起作用

时间:2014-07-15 03:52:16

标签: php html css wordpress

我正在尝试为Wordpress主题创建自定义联系表单页面。但是在我完成代码的过程中,我页面上的错误消息不会显示出来。

此外,我正在localhost计算机上运行以测试此自定义页面,似乎它不会将我的消息发送到我的Wordpress安装中设置的ADMIN EMAIL。

这是我的WP自定义联系表单上的代码段:

<?php 
    /* Template Name: Contact Page */
?>


<?php 

    // Function for email address validation
    function isEmail($verify_email) {

        return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$verify_email));

    }

    $error_name = false;
    $error_email = false;
    $error_subject = false;
    $error_message = false;


    if (isset($_POST['submit'])) {
        // Initialize the variables
        $contact_name = '';
        $contact_email = '';
        $contact_subject = '';
        $contact_message = '';


        // Get the name
        if (trim($_POST['contact_name']) === '') {
            $error_name = true;
        } else {
            $name = trim($_POST['contact_name']);
        }

        // Get the email
        if (trim($_POST['contact_email']) === '' || !isEmail($_POST['contact_email'])) {
            $error_email = true;
        } else {
            $email = trim($_POST['contact_email']);
        }


        // Check if we have errors
        if (!$error_name && !$error_email && !$error_subject && !$error_message) {
            // Get the receiver email from the WP admin panel
            $receiver_email = get_option('admin_email');

            $subject = "Message from $contact_name";
            $body = "You have a new quote request from $contact_name. Project details:" . PHP_EOL . PHP_EOL;
            $body .= PHP_EOL . PHP_EOL;

            $headers = "From: $contact_email" . PHP_EOL;
            $headers .= "Reply-To: $contact_email" . PHP_EOL;
            $headers .= "MIME-Version: 1.0" . PHP_EOL;
            $headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
            $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

            // If all is good, we send the email
            if (mail($receiver_email, $subject, $body, $headers)) {
                $email_sent = true;
            } else {
                $email_sent_error = true;
            }
        }
    }

?>



<?php  get_header(); ?>

<!-- BLOG AREA -->
<section>
        <hr class="no-margin" />

        <div class="blog-container section-content">
            <div class="container">
                <div class="row">
            <div class="col-md-8">
                <div class="box-layer custom-padding">


                    <div class="align-center">
                        <h2>We want to hear from you!</h2>
                        <p>If you are seeking to contact us, please fill up the form below. If you want to advertise or be partner with us just inform us on the message box below. </p>
                      <p>Thank you so much for your support!
                      <br/>We really appreciate!</p>




                <?php if (isset($email_sent) && $email_sent == true) : ?>

                    <h2>Success!</h2>
                    <p>You have successfully sent the quote request. I'll get back to you as soon as possible.</p>

                <?php elseif (isset($email_sent_error) && $email_sent_error == true) : ?>

                    <h2>There was an error!</h2>
                    <p>Unfortunately, there was an error while trying to send the email. Please try again.</p>

                <?php else : ?>



                        <form action="<?php the_permalink(); ?>" method="POST" class="general-form" novalidate>
                            <p <?php if ($error_name) echo 'class="error"'; ?>><input name="contact_name" id="contact_name" class="form-control" placeholder="Your Name.." type="text" value="<?php if (isset($_POST['contact_name'])) echo $_POST['contact_name']; ?>" /></p>
                            <p <?php if ($error_email) echo 'class="error"'; ?>><input name="contact_email" id="contact_email" class="form-control" placeholder="Your Email.." type="email" value="<?php if (isset($_POST['contact_email'])) echo $_POST['contact_email']; ?>" /></p>
                            <p <?php if ($error_subject) echo 'class="error"'; ?>><input name="contact_subject" id="contact_subject" class="form-control" placeholder="Your Subject.." type="text" value="<?php if (isset($_POST['contact_subject'])) echo $_POST['contact_subject']; ?>"/></p>
                            <p <?php if ($error_message) echo 'class="error"'; ?>><textarea  name="contact_message"  id="contact_message" class="form-control" placeholder="Write your comment here.." rows="4" cols="100"><?php if (isset($_POST['contact_message'])) echo $_POST['contact_message']; ?></textarea></p>
                            <input class="btn btn-primary no-border" name="submit" type="submit" id="submit" value="Send Message">
                        </form>
                            <?php endif; ?>


                    </div>


                </div>


<!-- RELATED ARTICLE AREA -->



                </div>




                    <aside>
            <!-- SIDEBAR AREA -->
                            <div class="col-md-3 col-md-offset-1 margin-sidebar">
                            <?php get_sidebar(); ?>
                            </div> 
                    </aside>


</section>



<?php  get_footer(); ?>

有什么想法吗?或者如果你看到一些错误,有没有办法可以纠正我的代码?

3 个答案:

答案 0 :(得分:0)

让你入门

  1. 确保wp-config.php文件中的WP_DEBUG设置为true。
  2. 您可以通过mail()(已解答here
  3. 获取最后一个错误
  4. 使用wordpress'功能(如果可用),在这种情况下wp_mail随时可用。
  5. 有wordpress插件,如重力形式或联系表格7,可以完成任务。
  6. 使用过滤器和操作。
  7. 不得不发帖作为答案,暂不予评论。

    HTH

答案 1 :(得分:0)

如果您想获取管理员电子邮件地址,则必须执行以下操作:

$bloginfo = get_bloginfo( 'admin_email' );

get_option('admin_email')仅在您在主题设置中保存任何内容时有效,可能现在为空。

顺便说一句,您可以找到许多专业的,随时可用的解决方案,例如WordPress Contact Form Slider

答案 2 :(得分:0)

<?php
/*
Plugin Name:Contact Form Plugin
Plugin URI: http://example.com
Description: Simple non-bloated WordPress Contact Form
Version: 1.0
Author: Agbonghama Collins
Author URI: http://w3guy.com
*/
if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');
        define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
        include( MY_PLUGIN_PATH . 'form/form.php');
        include( MY_PLUGIN_PATH . 'admin/admin-menu.php');
        include( MY_PLUGIN_PATH . 'database/table.php');
?>
Form.php
====================
<?php
function form()
{
    echo '<form action="'.esc_url($_SERVER['REQUEST_URI']).'"  method="post">';
    echo '<p>';
    echo 'Your First Name:';
    echo '<br/>';
    echo '<input type="text" name="fname" required/>';
    echo '<br/>';
    echo 'your Email-Id:';
    echo '<br/>';
    echo '<input type="email" name="email"  required/>';
    echo '<br/>';
    echo 'Your Subject:';
    echo '<br/>';
    echo '<input type="text" name="subject"  required/>';
    echo '<br/>';
    echo 'Your Message:';
    echo '<br/>';
    echo '<textarea name="message" cols="30" rows="4" required></textarea>';
    echo '<br/>';
    echo '<input type="submit" name="submit" value="Submit"/>';
    echo '</form>';         
}
if(isset($_REQUEST['submit']))
{
  $name = $_POST['fname'];
  $email = $_POST['email'];
   $subject=$_POST['subject'];
   $msg=$_POST['message'];
   function insertuser($name,$email,$msg,$subject){
    global $wpdb;
    $table_name = $wpdb->prefix . "contactinfo";
    $wpdb->insert($table_name, array('name' => $name, 'email' => $email , 'message'=> $msg , 'subject' => $subject) ); 
}
insertuser($name,$email,$msg,$subject);
}
function shortcode()
{
   form();  
}
add_shortcode('contact-form','shortcode');
?>
table.php
===================
<?php
global $wpdb; 
  $table_name = $wpdb->prefix . "contactinfo";
  $charset_collate = $wpdb->get_charset_collate();
   if( $wpdb->get_var ('SHOW TABLE LINK' .$table_name) != $table_name )
   {
$sql = "CREATE TABLE $table_name (
     id mediumint(9) NOT NULL AUTO_INCREMENT,
     name varchar(50) NOT NULL,
     email varchar(40) NOT NULL,
     message text NOT NULL,
     subject text NOT NULL,
     PRIMARY KEY  (id)
   ) $charset_collate;";
   require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
   dbDelta( $sql );  
   }
?>