I’m wondering how to create differents wp_mail_from
and wp_mail_from_name
for specific actions that use the built-in mail system in WordPress.
For example, when a new comment comes, notify the user using this:
function comments_new_mail_from($old) {return 'comments@example.com';}
function comments_new_mail_from_name($old) {return 'Comments at Example.com';}
add_filter('wp_mail_from', 'comments_new_mail_from');
add_filter('wp_mail_from_name', 'comments_new_mail_from_name');
But when a new user registers, use:
function users_new_mail_from($old) {return 'users@example.com';}
function users_new_mail_from_name($old) {return 'Users at Example.com';}
add_filter('wp_mail_from', 'users_new_mail_from');
add_filter('wp_mail_from_name', 'users_new_mail_from_name');
I want to do this to figure out the way to do this properly and avoid any problems with plugins using the built-in mail system that WordPress have.
There are two ways:
Inspect the original value, and if it specific for this case return a specific new value.
Chain the filters: register the mail filter only on specific hooks. So you have to find a hook that happens before
wp_mail()
is called.Simple example, not tested, just a guide:
The difficult part is finding the proper hook.
For example when a new comment has been written, the function
wp_notify_moderator()
is called. There is no really good hook in that function, but it calls â¦â¦ early. That again fires the hook
pre_option_moderation_notify
, and that is where we can start our filter. You have to search through the core code to find the best start hook for all cases, but usually there is always something.