6th Aug

WordPress Contact Form Plug-in With Attachment Feature

Post By admin | Find Under: PHP, Web Development, Wordpress

7

There are lots of plug-ins available for WordPress Contact Form. But you can create your own contact form plug-in with file attachment feature. It is a simple procedure to start writing plug-in codes from scratch.
Contact-Us plug-in


Step1: Create a php file named ‘contact-form-plugin.php’.
Step 2: In the header position of this file put the following author’s information codes.

  /*
Plugin Name: Contact Form with Attachment Feature
Plugin URI: http://www.wavesdream.com
Description:  WordPress Contact Form Plugin with Attachment Feature
Author: Sanjay Bhowmick
Version: 1.0
Author URI: http://www.wavesdream.com
you have to put '[contact_form]' in any page in HTML mode after activating the plugin
*/
  

Step 3: Here is the full source codes of the file. You can put the following codes in you php file.

<?php
/*
Plugin Name: Contact Form with Attachment Feature
Plugin URI: http://www.wavesdream.com
Description:  WordPress Contact Form Plugin with Attachment Feature
Author: Sanjay Bhowmick
Version: 1.0
Author URI: http://www.wavesdream.com
you have to put '[contact_form]' in any page in HTML mode after activating the plugin
*/

function contact_form_markup() {

$form_action    = get_permalink();
$author_default = $_COOKIE['comment_author_'.COOKIEHASH];
$email_default  = $_COOKIE['comment_author_email_'.COOKIEHASH];

if ( ($_SESSION['contact_form_success']) ) {
$contact_form_success = '<p style="color: green"><strong>Thank you for contacting us. We will get back to you soon</strong></p>';
unset($_SESSION['contact_form_success']);
}

$markup = <<<EOT
<div id="commentform" class="attachmentForm"><h3>Please fill the form below</h3>

	{$contact_form_success}

   <form onsubmit="return validateForm(this);" action="{$form_action}" method="post" enctype="multipart/form-data" style="text-align: left">

   <label for="author">Your Name<span class="red">*</span></label><input type="text" name="author" id="author" value="{$author_default}" size="22" />
   <br class="clear_contact"/>
   <label for="email">Your E-mail<span class="red">*</span></label><input type="text" name="email" id="email" value="{$email_default}" size="22" />
   <br class="clear_contact"/>
   <label for="subject">Subject<span class="red">*</span></label><input type="text" name="subject" id="subject" value="" size="22" />
   <br class="clear_contact"/>
   <label for="subject">Your Message</label>
   <textarea name="message" id="message" cols="40%" rows="5"></textarea>
   <br class="clear_contact"/>
   <p>
   <label for="attachment">Browse Files</label>
    <input type="file" name="attachment" id="attachment" />
	</p>
   <br class="clear_contact"/>
   <input name="send" type="submit" id="send" value="Send" />
   <br class="clear_contact"/>
   <input type="hidden" name="contact_form_submitted" value="1">

   </form>

</div>

EOT;

return $markup;

}

add_shortcode('contact_form', 'contact_form_markup');

function contact_form_process() {

session_start();

 if ( !isset($_POST['contact_form_submitted']) ) return;

 $author  = ( isset($_POST['author']) )  ? trim(strip_tags($_POST['author'])) : null;
 $email   = ( isset($_POST['email']) )   ? trim(strip_tags($_POST['email'])) : null;
 $subject = ( isset($_POST['subject']) ) ? trim(strip_tags($_POST['subject'])) : null;
 $message = ( isset($_POST['message']) ) ? trim(strip_tags($_POST['message'])) : null;

 if ( $author == '' ) wp_die('Error: please fill the required field (name).');
 if ( !is_email($email) ) wp_die('Error: please enter a valid email address.');
 if ( $subject == '' ) wp_die('Error: please fill the required field (subject).');

require_once ABSPATH . WPINC . '/class-phpmailer.php';
$mail_to_send = new PHPMailer();

$mail_to_send->FromName = $author;
$mail_to_send->From     = $email;
$mail_to_send->Subject  = $subject;
$mail_to_send->Body     = $message;

$mail_to_send->AddReplyTo($email);
$mail_to_send->AddAddress('sanjay.wavesdream@gmail.com'); //contact form destination e-mail

if ( !$_FILES['attachment']['error'] == 4 ) { //something was send

	if ( $_FILES['attachment']['error'] == 0 && is_uploaded_file($_FILES['attachment']['tmp_name']) )

		$mail_to_send->AddAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name']);

	else 

		wp_die('Error: Sorry, error occured this time to upload your attachment.');

}

if ( !$mail_to_send->Send() ) wp_die('Error: Email sending failed. Error Code: - status code: ' . $mail_to_send->ErrorInfo);

$_SESSION['contact_form_success'] = 1;

 header('Location: ' . $_SERVER['HTTP_REFERER']);
 exit();

} 

add_action('init', 'contact_form_process');

function contact_form_js() { ?>
1
<?php }

add_action('wp_head', 'contact_form_js');

?>
<style>
.attachmentForm form label {
  width:120px;
  display:block;
  float:left;
}
.clear_contact {
  clear:both;
  padding:5px;
  display:block;
  float:none;
  font-size:1px;
  margin:0px;
  padding:0px;
}
.red {
  font-size:18px;
  color:#FF0000;
  }
</style>

Step 4: Change your Email address in the following line of the code:

$mail_to_send->AddAddress('sanjay.wavesdream@gmail.com'); //contact form destination e-mail

Step 5: Now activate this Contact form plug-in from the Plug-in Admin Panel.
Step 6: Put the code [contact_form] in the HTML mode of your Contact Page.

Everything is done and enjoy with this Plug-in.

Click here to download the Contact Form with Attachment feature Plug-in.

If you think this article is useful then you can share this post.

7 Responses to “WordPress Contact Form Plug-in With Attachment Feature”

  1. kolombaru says:

    it's not working. i have message in admin window like this:

    Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at C:xampphtdocswordpresswp-contentpluginscontact-form-plugincontact-form-plugin.php:158) in C:xampphtdocswordpresswp-contentpluginscontact-form-plugincontact-form-plugin.php on line 62

    Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at C:xampphtdocswordpresswp-contentpluginscontact-form-plugincontact-form-plugin.php:158) in C:xampphtdocswordpresswp-contentpluginscontact-form-plugincontact-form-plugin.php on line 62

    what have id do??

    thanks for your help, Im waiting for ur answer….

  2. Katty Stone says:

    It was rather interesting for me to read that blog. Thanx for it. I like such topics and everything connected to this matter. I would like to read a bit more soon.

    Katty Stone

    indian escort service london

  3. admin says:

    Thanks dude :) I am thinking to upgrade the look & feel. Any suggestion?

  4. Edward says:

    This post is really interesting, and it relates to a project am working on. I am trying to add an attach file option (like the one in an email account) to my website. I have the html code for the page but I do not know how to create an attachment option. Would you be willing to help me out? Thanks

  5. Chris says:

    Hi did you work out the problem definied in the first comment?

  6. Very nice and useful plugin, thanks !

  7. Amy L. Adler says:

    This is a wonderful addition to the WP plugin pantheon. I struggled to find a plugin that would enable file uploads–so glad I came across your site!

    Amy

Leave a Reply