1

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?

3
  • 1
    Where is the form displayed? Jun 29, 2013 at 19:12
  • @OneTrickPony : in a text widget, per a comment to my answer.
    – s_ha_dum
    Jun 29, 2013 at 23:04
  • simply you can get admin email using this : get_bloginfo('admin_email')
    – alright
    Oct 14, 2019 at 7:21

2 Answers 2

0

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.

10
  • 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, 2013 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, 2013 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, 2013 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, 2013 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, 2013 at 23:09
23

To get WordPress admin email simply do:

$admin_email = get_option('admin_email');
3
  • They already have this in their code, which doesn't work for the reasons described in the accepted answer. Jul 24, 2018 at 14:35
  • 5
    Perhaps for the guy who asked, but not for the 18k people who came here looking for this answer and didn't find one, right? Jul 24, 2018 at 14:36
  • 1
    Yeah this question was badly titled, leading a lot of people here who only wanted this simple line of code.
    – Gavin
    Sep 21, 2019 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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