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?
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 edited Jan 10, 2020 at 14:18 Alexander Holsgrove 1,9091 gold badge15 silver badges25 bronze badges asked Jun 29, 2013 at 18:48 ForzaForza 1941 gold badge1 silver badge10 bronze badges 3- 1 Where is the form displayed? – onetrickpony Commented Jun 29, 2013 at 19:12
- @OneTrickPony : in a text widget, per a comment to my answer. – s_ha_dum Commented Jun 29, 2013 at 23:04
- simply you can get admin email using this : get_bloginfo('admin_email') – alright Commented Oct 14, 2019 at 7:21
2 Answers
Reset to default 0Merely 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.
To get WordPress admin email simply do:
$admin_email = get_option('admin_email');