My PHP knowledge is very basic. I've built a child theme and the functions.php is all from bits and pieces found on the web. All works great at the moment, and I've reused it on another website. But in several places I had to manually input blog name, email address, etc. What I want is to make it reusable without any further interventions over it. I've covered all problems, but one: changing the default wordpress@example.com email.
// Changing default wordpress email settings
add_filter('wp_mail_from', 'new_mail_from');
function new_mail_from($old) {
return 'notifications@example.com';
}
This works, although to make it reusable without intervention I need to somehow retrieve the site's URL without http://
and www
. I've made a test page and used:
$my_site = str_replace('www.','', $_SERVER['SERVER_NAME']);
echo 'notifications@'.$my_site;
It worked on the test.php
file, but not in Wordpress' functions.php
. The mail is sent and received, but the sender is 'notifications@' with nothing after @. I've used it as
return 'notifications@'.@my_site;
I've tried another approach:
$custom_email = 'notifications@'.str_replace('www.','', $_SERVER['SERVER_NAME']);
function new_mail_from($old) {
return $custom_email;
}
This one doesn't show any sender at all, it's from "unknown sender".
I've tried to work with site_url()
instead of $_SERVER
, but I haven't managed to make it work either. I didn't tried using home_url()
because maybe in some cases home_url()
will use a custom page (like a landing page).
Is there a way to solve this problem I have?
Thank you.