duanhuan7750 2014-12-04 00:08
浏览 116

Wordpress site_url字符串替换

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.

  • 写回答

1条回答 默认 最新

  • doubiao9775 2014-12-04 21:06
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站