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'm building a contactform that needs to send all queries to the admin mail adress.

The php file which processes the entered data and sends the email has only contains this code:

<?php
$admin_email = get_option('admin_email');

if($_POST["name"] != "" && $_POST["contact"] != ""  && $_POST["comments"] != "")
{
    echo "Uw bericht is verzonden! ";
    $name = $_POST["name"];
    $contactOption = $_POST["contactoption"];

    $msg = "Bericht afkomstig van: \n";
    $msg .= "Naam: " .$name;
    $msg .= "\nE-mail of Telefoonnummer: " .$contactOption;
    $msg .= "\n\n";
    $msg .= "Bericht: \n";
    $msg .= $_POST["comments"];
    $msg .= "\n\n";

    $subject = utf8_decode("Bericht via Contactformulier");
    $headers = utf8_decode("From: " .$name."\r\n");

    mail($mailTo, $subject, utf8_decode($msg), $headers);

}
?>

The problem is, I cannot use get_option or get_bloginfo to get the admin mail adress. This allways returns a fatal error.

This file resists in my childtheme folder. Why can't I use these functions? I've also tried adding get_header() and get_footer() to the file, but these functions are also not recognized.

I'm relatively new to wordpress. Am I missing something obvious here?

share|improve this question
1  
Where is the form displayed? –  onetrickpony Jun 29 '13 at 19:12
    
@OneTrickPony : in a text widget, per a comment to my answer. –  s_ha_dum Jun 29 '13 at 23:04

1 Answer 1

up vote 0 down vote accepted

Merely having a file in the theme folder does not mean that WordPress will load the file, or that when accessed the file will be loaded in a WordPress context.

There are two ways to approach this that I can think of:

In order to use WordPress functions WordPress needs to load. The easiest way to do that is to let WordPress load the file. Put your code above in the same template that holds your form-- presumably that is accessible so WordPress knows about the page. Just submit the form to the same page instead of to a different one. In the absence of strong reasons to do otherwise, this is what I would do.

You could also use the AJAX API to process your form.

Note: There is a kind-of a hack to load WordPress in external files by including wp-load.php but don't mess with that unless you have a very good idea what you are doing.

Edit:

Based on additional information, I now think a better solution is to create a proper widget for the form.

class Form_Widget_wpse_104728 extends WP_Widget {
  function __construct() {
    $opts = array(
      'description' => 'Display and Process My Form'
    );
    parent::WP_Widget(
      'my-form-content',
      'Some PHP',
      $opts
    );
  }
  function widget($args,$instance) {
    // PHP goes here
    // Your code to process the form
    // Your form itself
  }
}
function register_my_widgets() {
  register_widget('Form_Widget_wpse_104728');
}
add_action('widgets_init','register_my_widgets');

Even better would be to put the whole thing into a plugin so that you can process the form independently on a hook early in the page load.

share|improve this answer
    
For your first solution, do you mean that I can put this code in my page.php? The page that holds the form is no physical php file. Lemme know if that is what you meant. In the meantime I'll have a look at the AJAX API and wp-load hack :) –  Forza Jun 29 '13 at 22:11
    
You can put the code in the template file that processes the form-- preferably a custom template just for the form. Your form is not a shortcode is it? –  s_ha_dum Jun 29 '13 at 22:14
    
No the form resides in a textwidget inside the (pagebuilder)[wordpress.org/plugins/siteorigin-panels/] plugin. But I will try to make a template file. I'll let you know if that works :) –  Forza Jun 29 '13 at 22:55
    
I don't think this is gonna work. Do you want to put the whole contactform in the new template or just this code I posted above? The whole contactform is not gonna work as I can't just call the template from whitin a textwidget :P –  Forza Jun 29 '13 at 23:05
    
The fact that you form is in a text widget complicates things. The AJAX solution is the better of the two-- that you are dealing with a widget counts as "strong reasons to do otherwise". Still, this is sort of a mess given that text widget curve ball. I will edit the answer with what now seems like the best solution. –  s_ha_dum Jun 29 '13 at 23:09

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.