Fatal error when trying to get admin email on contact form

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:

Read More
<?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 .= "nn";
    $msg .= "Bericht: n";
    $msg .= $_POST["comments"];
    $msg .= "nn";

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

    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?

Related posts

2 comments

  1. 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.

  2. To get WordPress admin email simply do:

    $admin_email = get_option('admin_email');
    

Comments are closed.