Wrap a php parser file in WordPress theme

I created a form parser for a WordPress site that would parse the contents of a posted form, and display a success page. I used pure php instead of trying to use a WordPress template or theme. As a downside, the displayed page would not show any WordPress theme.

So I found a solution to import the theme for the php, and this is by including the theme’s header and footer in the php file.

The complete form parser is included below:

<?php

// If title is not displayed before loading the header, WordPress displays "Page not found" as the title

echo "<head>
<title>Your form has been submitted</title>
</head>";

// Include the Main WordPress blog header
include $_SERVER['DOCUMENT_ROOT']."/wp-blog-header.php";

// Include the specific theme header you need
include $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/studiopress/genesis/header.php";

// Your custom code STARTS here

// My code includes two functions. 
// The first part is the background activity which parses the form, checks for errors and then mails it if there arent any errors
// The second part is html code that is displayed to the user to inform them that the form has been submitted.

if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "[email protected]";
    $email_subject = "Someone has submitted feedback on your website";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers

$headers = 'From: '.'[email protected]'."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<?php
// Second part starts.
// This part is displayed.

echo "
<h2>
Your form has been submitted
</h2>";

echo "<h4>Dear $first_name,</h4>
<p>Your feedback has been received by us and will be forwarded to the site administrator. You will receive your login details or a response soon.</p>";

echo "<br /><br /><br /><br /><br /><br /><br />
Click <a href=\"http://www.site.com\">here</a> to return to the site.";
// END of custom code

?> 

<?php
}
include $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/studiopress/genesis/footer.php";
?>[/code]
The following is the actual form:
You can use the following form to contact the administrator of this site:

<form action="../phpscripts/send_form_email1.php" method="post" name="contactform">Note: If you're requesting login rights, please add information on how you're related to the family.&nbsp;
<table width="450px">
<tbody>
<tr>
<td valign="top"><label for="first_name">First Name *</label></td>
<td valign="top"><input type="text" maxlength="50" name="first_name" size="30" /></td>
</tr>
<tr>
<td valign="top"><label for="last_name">Last Name *</label></td>
<td valign="top"><input type="text" maxlength="50" name="last_name" size="30" /></td>
</tr>
<tr>
<td valign="top"><label for="email">Email Address *</label></td>
<td valign="top"><input type="text" maxlength="80" name="email" size="30" /></td>
</tr>
<tr>
<td valign="top"><label for="telephone">Telephone Number</label></td>
<td valign="top"><input type="text" maxlength="30" name="telephone" size="30" /></td>
</tr>
<tr>
<td valign="top"><label for="comments">Comments *</label></td>
<td valign="top"><textarea cols="30" maxlength="1000" name="comments" rows="6"></textarea></td>
</tr>
<tr>
<td></td>
<td style="text-align: left;" colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</tbody>
</table>
</form>[/code]
Version 0.2

I made it generic and detect the proper active theme:
&lt;?php

// If title is not displayed before loading the header, WordPress displays "Page not found" as the title

echo "&lt;head&gt;
&lt;title&gt;Your feedback has been submitted&lt;/title&gt;
&lt;/head&gt;";

// Include the Main WordPress blog header
include $_SERVER['DOCUMENT_ROOT']."/wp-blog-header.php";

//bloginfo(‘template_url’);
$homeurl=home_url();
$ddir= get_bloginfo( 'template_directory');
$current_theme_relative_path=substr_replace($ddir, "", 0, strlen($homeurl));
//echo "&lt;br/&gt;The relative path to the currently active theme is ".$newss;

// Include the specific theme header you need
//echo "Full path to header is ".$_SERVER['DOCUMENT_ROOT'].$newss."/header.php";
include $_SERVER['DOCUMENT_ROOT'].$current_theme_relative_path."/header.php";

// Your custom code STARTS here

// My code includes two functions. 
// The first part is the background activity which parses the form, checks for errors and then mails it if there arent any errors
// The second part is html code that is displayed to the user to inform them that the form has been submitted.

if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "[email protected]";
    $email_subject = "Someone has submitted feedback on your website";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.&lt;br /&gt;&lt;br /&gt;";
        echo $error."&lt;br /&gt;&lt;br /&gt;";
        echo "Please go back and fix these errors.&lt;br /&gt;&lt;br /&gt;";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.&lt;br /&gt;';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.&lt;br /&gt;';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.&lt;br /&gt;';
  }
  if(strlen($comments) &lt; 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.&lt;br /&gt;';
  }
  if(strlen($error_message) &gt; 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers

$headers = 'From: '.'[email protected]'."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?&gt;

&lt;?php
// Second part starts.
// This part is displayed.

echo "
&lt;h2&gt;
Your feedback/request has been submitted
&lt;/h2&gt;";

echo "&lt;h4&gt;Dear $first_name,&lt;/h4&gt;
&lt;p&gt;Your feedback has been received by us and will be forwarded to the site administrator. 
&lt;br/&gt;
You will receive your login details or a response soon.&lt;/p&gt;";

echo "&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
Click &lt;a href=\"http://www.vettathu.com\"&gt;here&lt;/a&gt; to return to the site.";
// END of custom code

//Useful wordpress functions
/*
echo "The current theme name is".get_current_theme()."&lt;br/&gt;";

echo "&lt;br/&gt;";
echo "The dir is ".$ddir."&lt;br/&gt;";

echo "The home url is".$homeurl."&lt;br/&gt;";
*/
//$newdirst=str_replace($homeurl,".",$temp_ddir);

/*
echo "Length of whole is ".strlen($ddir);
echo "Length of substring is ".strlen($homeurl);

*/

?&gt; 

&lt;?php
}
include $_SERVER['DOCUMENT_ROOT'].$current_theme_relative_path."/footer.php";

?&gt;