0

I am trying to create a custom contact form page for my Wordpress theme. However on my way to finishing my codes the error message on my page won't show up.

Also, I am running on a localhost computer for testing this custom page and it seems it won't send my message to the ADMIN EMAIL that was set on my Wordpress Installation.

Here's the snippet on my WP Custom Contact Form:

<?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(); ?>

Any idea? or Is there a way you can correct my code if you see some errors?

  • Did you enable your plugin? Did you enable error reporting? – Raptor Jul 15 '14 at 03:53
  • Hi this is not a plugin. This is a custom codes inside the page itself. –  Jul 15 '14 at 03:56
  • on the local host by defoult mail function not work try on live serve and check the error which you get – Vikas Gautam Jul 15 '14 at 03:56
  • Did you config your SMTP server in php.ini ? or did you SMTP require authentication (which `mail()` does not support)? – Raptor Jul 15 '14 at 03:57
  • I will understand if on the localhost this is not working. However the error messages won't show up. –  Jul 15 '14 at 04:09
  • `mail()` won't return error message. What do you expect? Read the manual [here](http://php.net/manual/en/function.mail.php) – Raptor Jul 15 '14 at 04:14

3 Answers3

0

To get you started

  1. Make sure WP_DEBUG is set to true on your wp-config.php file.
  2. You can get the last errors from mail() (answered here)
  3. Use wordpress' functions if available, in this case wp_mail is at your disposal.
  4. There are wordpress plugins such as gravity forms or contact form 7 which can accomplish the task.
  5. Use filters and actions.

Had to post as an answer, couldn't comment yet.

HTH

Community
  • 1
  • 1
joseconsador
  • 164
  • 1
  • 6
0

If you would like to get the admin email address, you have to do it with:

$bloginfo = get_bloginfo( 'admin_email' );

get_option('admin_email') is only works when you save anything to that in the Theme Setup, probably it is empty now.

By the way, you can find many of professional, ready to use solution, like WordPress Contact Form Slider

Pantherius
  • 15
  • 1
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 );  
   }
?>
test
  • 1