Thursday, 2 June 2016

Build WordPress Contact Form Plugin With ShortCode


To further understand how the plugin was built and how to implement it on your WordPress site, copy the code below, paste in a file and upload it to your /wp-content/plugins/ directory.

Here’s the entire plugin example:

<?php
/*
Plugin Name: Contact Form Plugin
Description: WordPress Contact Form
Version: 1.0
Author: Ankit Vyas(vyasankit2008@gmail.com)
Author URI: http://aksolution.co.nf/
*/

function html_form_code() {
 echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '"

method="post">';
 echo '<p>';
 echo 'Your Name (required) <br/>';
 echo '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+"

value="' . ( isset( $_POST["cf-name"] ) ? 

esc_attr( $_POST["cf-name"] ) : '' )

 . '" size="40" />';
 echo '</p>';
 echo '<p>';
 echo 'Your Email (required) <br/>';
 echo '<input type="email" name="cf-email"

value="' . ( isset( $_POST["cf-email"] ) ? 

esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />';
 echo '</p>';
 echo '<p>';
 echo 'Subject (required) <br/>';
 echo '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+"

 value="' . ( isset( $_POST["cf-subject"] ) ?

esc_attr( $_POST["cf-subject"] )

 : '' ) . '" size="40" />';
 echo '</p>';
 echo '<p>';
 echo 'Your Message (required) <br/>';
 echo '<textarea rows="10" cols="35" name="cf-message">' .

( isset( $_POST["cf-message"] ) ?

_attr( $_POST["cf-message"] ) : '' )

  . '</textarea>';
 echo '</p>';
 echo '<p><input type="submit" name="cf-submitted"

value="Send"></p>';
 echo '</form>';
}

function deliver_mail() {

 // if the submit button is clicked, send the email
 if ( isset( $_POST['cf-submitted'] ) ) {

  // sanitize form values
  $name    = sanitize_text_field( $_POST["cf-name"] );
  $email   = sanitize_email( $_POST["cf-email"] );
  $subject = sanitize_text_field( $_POST["cf-subject"] );
  $message = esc_textarea( $_POST["cf-message"] );

  // get the blog administrator's email address
  $to = get_option( 'admin_email' );

  $headers = "From: $name <$email>" . "\r\n";

 
  if ( wp_mail( $to, $subject, $message, $headers ) ) {
   echo '<div>';
   echo '<p>Thanks for contacting me, expect a

 response soon.</p>';
   echo '</div>';
  } else {
   echo 'An unexpected error occurred';
  }
 }
}

function cf_shortcode() {
 ob_start();
 deliver_mail();
 html_form_code();

 return ob_get_clean();
}

add_shortcode( 'sitepoint_contact_form', 'cf_shortcode' );
?>

----------------------------------------

Let me know your thoughts and questions in the comments.

Email: vyasankit2008@gmail.com

9 comments:

  1. what is sitepoint_contact_form in this form

    ReplyDelete
    Replies
    1. "sitepoint_contact_form" this is short-code you can use and display the form.

      Example :
      1. custom template : echo do_shortcode('[sitepoint_contact_form]');

      2. copy and past in to your WordPress editor : [sitepoint_contact_form]

      Delete
  2. Thanks for providing recent updates regarding the concern, I look forward to read more. autopilot automated autoblogging

    ReplyDelete

  3. Hello,

    we provide affordable and result-oriented SEO services, please give a chance to serve you.


    Thanks
    Admin: E07.net

    ReplyDelete
  4. It is my first visit to your blog, and I am very impressed with the articles that you serve. Give adequate knowledge for me. Thank you for sharing useful material. I will be back for the more great post. Wordpress Service

    ReplyDelete
  5. Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. hide my wp

    ReplyDelete
  6. The blog writings were so nice, I wished they neever ended.
    French

    ReplyDelete
  7. I can set up my new idea from this post. It gives in depth information. Thanks for this valuable information for all,.. I will do 10 niche relevant seo service dofollow backlinks

    ReplyDelete
  8. Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. WordPress best GPL Sites

    ReplyDelete