· 5 years ago · Jun 25, 2020, 12:24 PM
1<?php
2
3function html_form_code() {
4
5
6 echo '<form action="' . esc_url($_SERVER['REQUEST_URI']) . '" method="post" class="wpcf7-form">';
7 ?>
8
9 <input type="hidden" id="g-token" name="g-token" />
10
11 <div class="input-group">
12 <label class="col-4"><span class="wpcf7-form-control-wrap text-469"><input type="text" name="cf-name" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" placeholder="Name*"></span></label><label class="col-4"><span class="wpcf7-form-control-wrap text-89"><input type="text" name="cf-companyname" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" placeholder="Company Name*"></span></label><label class="col-4"><span class="wpcf7-form-control-wrap text-175"><input type="text" name="cf-designation" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" placeholder="Designation*"></span></label>
13</div>
14
15<div class="input-group">
16 <label class="col-4"><span class="wpcf7-form-control-wrap text-232"><input type="text" name="cf-contact" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" placeholder="Contact No*"></span></label><label class="col-4"><span class="wpcf7-form-control-wrap email-958"><input type="email" name="cf-email" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email" aria-required="true" aria-invalid="false" placeholder="Email Address*"></span></label><label class="col-4"><span class="wpcf7-form-control-wrap menu-486"><select name="cf-menu" class="wpcf7-form-control wpcf7-select wpcf7-validates-as-required" aria-required="true" aria-invalid="false"><option value="Select an Option">Select an Option</option><option value="General Enquiry">General Enquiry</option><option value="Commercial">Commercial</option><option value="Residential">Residential</option></select></span></label></div>
17
18 <p><label> <span class="wpcf7-form-control-wrap textarea-677"><textarea name="cf-textarea" cols="40" rows="10" class="wpcf7-form-control wpcf7-textarea" aria-invalid="false" placeholder="Message*"></textarea></span> </label></p>
19<p align="center"><button class="btn btn-brand" name="cf-submitted">
20 <span class="btn-load"></span>
21 <span class="text">Send Enquiry</span>
22 </button></p>
23<div class="wpcf7-response-output wpcf7-display-none"></div></form>
24 <?php
25}
26
27
28
29
30function deliver_mail() {
31
32
33$absolute_path = __FILE__;
34$path_to_file = explode( 'wp-content', $absolute_path );
35$path_to_wp = $path_to_file[0];
36
37// Access WordPress
38require_once( $path_to_wp . '/wp-load.php' );
39
40
41 // if the submit button is clicked, send the email
42 if ( isset( $_POST['cf-submitted'] ) ) {
43
44
45
46 // sanitize form values
47
48 $name = sanitize_text_field( $_POST["cf-name"] );
49 $companyname = sanitize_text_field( $_POST["cf-companyname"] );
50 $designation = sanitize_text_field( $_POST["cf-designation"] );
51 $contact = sanitize_text_field( $_POST["cf-contact"] );
52 $email = sanitize_email( $_POST["cf-email"] );
53 $subject = sanitize_text_field( $_POST["cf-menu"] );
54
55
56
57
58 $message = 'Name: '. $name. "\r\n";
59 $message .= 'Company Name: '. $companyname. "\r\n";
60 $message .= 'Designation: '. $designation. "\r\n";
61 $message .= 'Contact No: '. $contact. "\r\n";
62 $message .= 'Email Address: '. $email. "\r\n";
63 $message .= 'Select an Option: '. $subject. "\r\n";
64 $message .= 'Message: '."\r\n". esc_textarea( $_POST["cf-textarea"] );
65
66 // get the blog administrator's email address
67
68 $to = 'mrarif.cse@gmail.com';
69
70 if ($subject == "General Enquiry"){
71 $to = "arif.iovision@gmail.com";
72 }
73
74 elseif ($subject == "Commercial"){
75 $to = "arifbabu587@gmail.com";
76 }
77
78 elseif ($subject == "Residential"){
79 $to = "info.mrarif@gmail.com";
80 }
81
82 else {
83 $to = 'mrarif.cse@gmail.com';
84 }
85
86 $secretKey = '6Ld8BJkUAAAAAIBV3qqMTohh9m1a3Lxr7r0Jqu_R';
87 $token = $_POST["g-token"];
88 $ip = $_SERVER['REMOTE_ADDR'];
89 $url = "https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$token."&remoteip=".$ip;
90 $request = file_get_contents($url);
91 $response = json_decode($request);
92
93
94 if($response->success)
95 {
96 ?>
97 <div class="vc_col-sm-12 text-left" style="color:#3e934f;font-size:16px;margin-top:20px;">
98 <?php
99 if ( wp_mail( $to, $subject, $message)) {
100 echo '<p>Thank you for your message. It has been sent.</p>';
101
102 } else {
103 echo 'An unexpected error occurred';
104 }
105 ?>
106 </div>
107 <?php
108 }
109 else
110 {
111 ?>
112 <div class="vc_col-sm-12 text-left" style="color:#00A4E1;font-size:16px;margin-top:20px;">
113
114 </div>
115 <?php
116 }
117 }
118}
119
120
121
122
123function cf_shortcode() {
124 ob_start();
125
126 html_form_code();
127 deliver_mail();
128 return ob_get_clean();
129}
130add_shortcode( 'sitepoint_contact_form', 'cf_shortcode' );