Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have some custom forms in my template pages. I'm using a form "action" to link to a formtoemail.php file. Everything works if I leave formtoemail.php as is, but I would like to call in my theme elements to style the page dynamically.

I have tried the following, but unfortunately get an error "Call to undefined function get_header()"

If I copy / paste the html from the page all works great, but I really would rather not have to update the html everytime. I'm sure there is a way to do this, but I'm not super PHP savvy. Can anyone help?

<?php

$my_email = "name@email.com";

/*

Enter the continue link to offer the user after the form is sent.  If you do not change this, your visitor will be given a continue link to your homepage.

If you do change it, remove the "/" symbol below and replace with the name of the page to link to, eg: "mypage.htm" or "http://www.elsewhere.com/page.htm"

*/

$continue = "/";

/*

Step 3:

Save this file (FormToEmail.php) and upload it together with your webpage containing the form to your webspace.  IMPORTANT - The file name is case sensitive!  You must save it exactly as it is named above!  Do not put this script in your cgi-bin directory (folder) it may not work from there.

THAT'S IT, FINISHED!

You do not need to make any changes below this line.

*/

$errors = array();

// Remove $_COOKIE elements from $_REQUEST.

if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}

// Check all fields for an email header.

function recursive_array_check_header($element_value)
{

global $set;

if(!is_array($element_value)){if(preg_match("/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i",$element_value)){$set = 1;}}
else
{

foreach($element_value as $value){if($set){break;} recursive_array_check_header($value);}

}

}

recursive_array_check_header($_REQUEST);

if($set){$errors[] = "You cannot send an email header";}

unset($set);

// Validate email field.

if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
{

if(preg_match("/(%0A|%0D|\n+|\r+|:)/i",$_REQUEST['email'])){$errors[] = "Email address may not contain a new line or a colon";}

$_REQUEST['email'] = trim($_REQUEST['email']);

if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}}

}

// Check referrer is from same site.

if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}

// Check for a blank form.

function recursive_array_check_blank($element_value)
{

global $set;

if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}
else
{

foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}

}

}

recursive_array_check_blank($_REQUEST);

if(!$set){$errors[] = "You cannot send a blank form";}

unset($set);

// Display any errors and exit if errors exist.

if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}

if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");}

// Build message.

function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}

$message = build_message($_REQUEST);

$message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."";

$message = stripslashes($message);

$subject = "Dealer Locator Form from site.com";

$headers = "From: site.com" . $_REQUEST['Name'];

mail($my_email,$subject,$message,$headers);

?>

<?php get_header(); ?>

<!--BKGD -->
<div class="blue"></div>
<!--BKGD -->

<!--Page Image / Title Area -->
<span class="hidden-xs">
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12">
<div id="title">
<h1 class="pagetitle"><?php the_title(); ?></h1>
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<small><p id="breadcrumbs">','</p></small>');
} ?>
</div>
</div></div></div>
</span>
<!--Page Image / Title Area -->

<!--Mobile Title Area -->
<div id="spacer"></div>
<!--Content Begin-->
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-lg-9">

<section>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-4">

  <img class="img-responsive" src="http://www.site.com/testarea/wp-content/themes/site/img/placeholder-img.jpg" width="262" height="222" />
  </div>  

<div class="col-lg-8 col-md-8 col-sm-8">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_content(); ?>
<div class="entry-links"><?php wp_link_pages(); ?></div>

<?php endwhile; endif; ?>
<?php edit_post_link(); ?>
</div>

</div>
</section>

  </div><!-- End col-lg-9 -->
  <div class="col-lg-3 visible-lg">
<?php 
 // Custom widget Area Start
 if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Dealer Search') ) : ?>
<?php endif;
// Custom widget Area End
?>
</div><!-- End col-lg-3-->
</div> <!-- End row -->
</div><!-- End col-lg-12-->
</div> <!-- End row -->
<div id="spacer"></div>

</div><!-- End Container -->
<div id="spacer"></div>
<?php get_footer(); ?>
share|improve this question

2 Answers 2

You need to include the wordpress config file:

require $_SERVER['DOCUMENT_ROOT'].'/wp-config.php';

OR use a page template if your form handler is in your theme. Take a look at using page template's in wordpress: http://codex.wordpress.org/Page_Templates

share|improve this answer
    
I'm not sure I understand what you mean about the wordpress config file. Also I am using page templates for the form pages referencing the formtoemail.php. Can you elaborate a little? –  streetfire Dec 3 '13 at 23:39
    
I've edited my answer but it looks to me like you just want to add a form to your default page, why not use a contact form plugin? –  Innate Dec 4 '13 at 7:45
    
Because I don't like the added bulk and styles they input. –  streetfire Dec 7 '13 at 16:12

Maybe consider the cloud (hosted) version of the script: http://formtoemail.com

share|improve this answer
    
how will that help with styling? –  Mark Kaplun Dec 24 '13 at 7:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.