1

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.

Anoop Asok
  • 1,311
  • 1
  • 8
  • 20
  • So `$my_site` is empty, what does that tell you? Also, take a look at PHP's `parse_url` function, avoiding `str_replace`. – Madbreaks Dec 04 '14 at 00:11
  • try `$_SERVER['HTTP_HOST']` instead. – Victory Dec 04 '14 at 00:14
  • **@Madbreaks** - The empty $my_site tells me nothing, unfortunately. I don't really know how PHP works, so I don't know what that should tell me. As I said, my PHP knowledge is very basic. I've started messing around with parse_url, nothing happened yet, but I hope I'll come up with something that works. Thank you. – Ovidiu Zeicu Dec 04 '14 at 11:41
  • **@Victory** - The result is the same as $_SERVER['SERVER_NAME'], nothing appears after @. – Ovidiu Zeicu Dec 04 '14 at 11:43

1 Answers1

0

Ok, with a bit of help from a friend, I've found something that works:

function new_mail_from($old) {
  $custom_email = 'notifications@'.str_replace('www.','', $_SERVER['SERVER_NAME']);
  return $custom_email;
}

So basically all I need is to put $custom_email inside the function.

@MadBreaks Now... I know your advice was to avoid str_replace, but I didn't manage to understand parse_url and how to use it. Why isn't str_replace a good choice?

@Victory $_SERVER['SERVER_NAME'] and $_SERVER['HTTP_HOST'] did for me the same thing. Could you please tell me what's the difference? Or pros and cons in using each of them in this situation? Thank you.

  • with str_replace, you might run into trouble with subdomains like new.example.com ( email is unlikely to be just for the subdomain ). That said, parse_url wont be enough there either. I'd try `get_option( 'admin_email' )` instead. – MSTannu Dec 04 '14 at 21:16
  • Found my answer here http://stackoverflow.com/questions/2297403/http-host-vs-server-name and I think I'll be using HTTP_HOST after all. Still didn't entirely understood all of it :P – Ovidiu Zeicu Dec 04 '14 at 21:39
  • **@MSTannu** You're further confusing me. :) I'm too novice for all this and I can't assimilate all of it in this short time. I'll definitely look `get_option('admin_email')` as an option and try to learn about it. Thank you. – Ovidiu Zeicu Dec 04 '14 at 21:43