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.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端