Beginner’s WordPress: A Privacy Policy and Contact Us Page

April 17, 2015 . by Aaron B

I’m sure you have seen both “Privacy Policy” and “Contact Us” pages on almost every website that you’ve ever visited. Both of these different pages are very important to your WordPress site. Below is some information that will explain to you the benefits of these pages, and some steps that you can take to add them to your website.

The “Contact Us” Page

A “Contact Us” page is exactly what it sounds like, a way for your readers or customers to contact you if they have any comments or questions. Having a “Contact Us” page is incredibly beneficial to your site as it allows your readers to get in touch with you without having to worry about being drowned out in a large comment section.

To create a “Contact Us” page with a built in contact form, you are going to need build a custom page template. To do this is a simple task, first you will need to add a new blank PHP file to your theme directory. Save this as contact_template.php, within the file you will need to add the following:

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

The reason behind this is it will automatically add this new page template to the drop down when adding a new page.

Select Page Template

Select Page Template

 

Below is an example you could use to build a “custom” contact us page, You can copy and paste this code into the new PHP file we made above. On line 46 you will need to look for this:  $emailTo = ‘youremail@yourdomain.co.uk’;  as you will need to change the email address in the ” so you get the email messages from the form.

 

<?php
/*
Template Name: Contact Us Template
*/
$nameError = '';
$emailError = '';
$commentError = '';
//If the form is submitted
if( isset( $_POST['submitted'] ) ) {

    //Check to see if the honeypot captcha field was filled in
    if( trim( $_POST['checking'] ) !== '' ) {
        $captchaError = true;
    } else {

        //Check to make sure that the name field is not empty
        if( trim( $_POST['contactName'] ) === '' ) {
            $nameError =  __( 'You forgot to enter your name.', 'AG' );
            $hasError = true;
        } else {
            $name = trim( $_POST['contactName'] );
        }

        //Check to make sure sure that a valid email address is submitted
        if( trim( $_POST['email'] ) === '' )  {
            $emailError = __( 'You forgot to enter your email address.', 'AG' );
            $hasError = true;
        } else if ( ! eregi( "^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email'] ) ) ) {
            $emailError = __( 'You entered an invalid email address.', 'AG' );
            $hasError = true;
        } else {
            $email = trim( $_POST['email'] );
        }

        //Check to make sure comments were entered
        if( trim( $_POST['comments'] ) === '' ) {
            $commentError = __( 'You forgot to enter your comments.', 'AG' );
            $hasError = true;
        } else {
            $comments = stripslashes( trim( $_POST['comments'] ) );
        }

        //If there is no error, send the email
        if( ! isset( $hasError ) ) {

            $emailTo = 'youremail@yourdomain.co.uk';// Change this email address to your email
            $subject = __( 'Contact Form Submission from ', 'AG' ).$name;
            $sendCopy = trim( $_POST['sendCopy'] );
            $body = __( "Name: $name \n\nEmail: $email \n\nComments: $comments", 'AG' );
            $headers = __( 'From: ', 'AG') . "$name <$email>" . "\r\n" . __( 'Reply-To: ', 'AG' ) . $email;

            wp_mail( $emailTo, $subject, $body, $headers );

        }
    }
}
?>
<script type="text/javascript">
    <!--//--><![CDATA[//><!--
        jQuery(document).ready(function() {
        jQuery( 'form#contactForm').submit(function() {
            jQuery( 'form#contactForm .error').remove();
            var hasError = false;
            jQuery( '.requiredField').each(function() {
                if(jQuery.trim(jQuery(this).val()) == '') {
                    var labelText = jQuery(this).prev( 'label').text();
                    jQuery(this).parent().append( '<span class="error"><?php _e( 'You forgot to enter your', 'AG' ); ?> '+labelText+'.</span>' );
                    jQuery(this).addClass( 'inputError' );
                    hasError = true;
                } else if(jQuery(this).hasClass( 'email')) {
                    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                    if(!emailReg.test(jQuery.trim(jQuery(this).val()))) {
                        var labelText = jQuery(this).prev( 'label').text();
                        jQuery(this).parent().append( '<span class="error"><?php _e( 'You entered an invalid', 'AG' ); ?> '+labelText+'.</span>' );
                        jQuery(this).addClass( 'inputError' );
                        hasError = true;
                    }
                }
            });
            if(!hasError) {
                var formInput = jQuery(this).serialize();
                jQuery.post(jQuery(this).attr( 'action'),formInput, function(data){
                    jQuery( 'form#contactForm').slideUp( "fast", function() {
                        jQuery(this).before( '<p class="tick"><?php _e( '<strong>Thanks!</strong> Your email was successfully sent.', 'AG' ); ?></p>' );
                    });
                });
            }

            return false;

        });
    });
    //-->!]]>
</script>

<?php get_header(); ?>

<section class="">
    <div class="inner">

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">

            <h1 class="entry-title"><?php the_title(); ?></h1>

            <div class="entry-content">

                <div class="contact-form clearfix">

                    <div style="width:48%; float:left;">
                        <?php the_content(); ?>
                    </div>

                    <div style="width:48%; float:left;">
                        <form action="<?php the_permalink(); ?>" id="contactForm" method="post">

                            <label for="contactName"><?php _e( 'Name:', 'AG' ); ?></label>

                            <input type="text" name="contactName" id="contactName"  style="width:100% " value="<?php if( isset( $_POST['contactName'] ) ) { echo esc_attr( $_POST['contactName'] ); } ?>" class="txt requiredField" />
                            <?php if($nameError != '') { ?>
                            <span class="error"><?php echo $nameError;?></span>
                            <?php } ?><br><br>

                            <label for="email"><?php _e( 'Email:', 'AG' ); ?></label>

                            <input type="text" name="email" id="email" style="width:100% " value="<?php if( isset( $_POST['email'] ) ) { echo esc_attr( $_POST['email'] ); } ?>" class="txt requiredField email" />
                            <?php if($emailError != '') { ?>
                            <span class="error"><?php echo $emailError;?></span>
                            <?php } ?><br><br>

                            <label for="comments"><?php _e( 'Message', 'AG' ); ?></label>

                            <textarea name="comments" id="commentsText" style="width: 100%; height: 234px;" class="requiredField"><?php if( isset( $_POST['comments'] ) ) { echo esc_textarea( $_POST['comments'] ); } ?></textarea>
                            <?php if( $commentError != '' ) { ?>
                            <span class="error"><?php echo $commentError; ?></span>
                            <?php } ?>

                            <br><br>


                            <li class="screenReader" style="display:none"><label for="checking" class="screenReader">If you want to submit this form, do not enter anything in this field</label><input type="text" name="checking" id="checking" class="screenReader" value="<?php if( isset( $_POST['checking'] ) ) { echo esc_attr( $_POST['checking'] ); } ?>" /></li>
                            <input type="hidden" name="submitted" id="submitted" value="true" />
                            <div class="block block-button"><input class="button" type="submit" value="Submit" /></div>

                        </form>
                    </div>
                   <div style="clear:both;"></div>


                </div>
            </div>
        </div>

        <?php endwhile; else: ?>

        <div id="post-0" <?php post_class() ?>>

            <h1 class="entry-title"><?php _e('Error 404 - Not Found', 'AG') ?></h1>

            <div class="entry-content">
                <p><?php _e("Sorry, but you are looking for something that isn't here.", "AG") ?></p>
                <?php get_search_form(); ?>
            </div>
        </div>

        <?php endif; ?>

    </div>
</section>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

This contact form has a built in honeypot captcha to stop the spam bots from sending you messages you don’t want. Now I hear you saying what is a honeypot captcha, well on line 142 there is a input box that only the spam bots can see. If this input has any text put into it the message will not get sent. PERFECT!

Now let’s move onto creating your “Privacy Policy” page.

The “Privacy Policy” Page

You may have found yourself wondering what the purpose of the “Privacy Policy” page is. Basically, a “Privacy Policy” page is a document that tells your readers what information you collect and what you do with it. There are a couple of different benefits in having a “Privacy Policy” page. The first is that it can increase the trust that your readers have in you by disclosing what you intend to do with their data. The second is that some jurisdictions are starting to require them for websites that operate businesses based on online shopping.
To write your “Privacy Policy” page you’re first going to need to add a new page entitled “Privacy Policy.” If you need to, don’t hesitate to re-read the section above to refresh your memory on how to do this.
Thankfully, there are a wide variety of places where you can find “Privacy Policy” page samples. A great example of a “Privacy Policy” that you can use as a free template is located here. Simply copy and paste the text from the website into your WordPress page editor area then fill in all of your relevant information.

Conclusion

Hopefully this article helped you to develop an appropriate “Contact Us” and “Privacy Policy” page for your website. If you still have any questions or concerns, feel free to contact us here. If you liked this article, comment below and let us know! If you want to learn more about WordPress feel free to read some of the other entries in this series!