钩子文档

wp_mail_from_name

💡 云策文档标注

概述

wp_mail_from_name 是一个 WordPress 过滤器,用于修改通过 wp_mail() 函数发送邮件时的“发件人名称”。它通常与 wp_mail_from 过滤器结合使用,以自定义完整的发件人地址格式。

关键要点

  • 过滤器名称:wp_mail_from_name,用于修改邮件发件人名称。
  • 参数:$from_name(字符串),表示原始的发件人名称,过滤器应返回修改后的字符串。
  • 与 wp_mail_from 过滤器配合使用,可设置如“名称 <email@example.com>”格式的发件人地址。
  • 注意:如果使用匿名函数添加过滤器,则无法通过 remove_filter() 移除。

代码示例

// 使用命名函数示例
add_filter( 'wp_mail_from_name', 'custom_wp_mail_from_name' );
function custom_wp_mail_from_name( $original_email_from ) {
    return 'WordPress Email System';
}

// 使用匿名函数示例(PHP 5.3.0+)
add_filter( 'wp_mail_from_name', function( $name ) {
    return 'WordPress Email System';
} );

注意事项

  • 在子域名站点中,可能需要同时使用 wp_mail_from 和 wp_mail_from_name 过滤器来确保邮件发送正常,避免国际客户无法发送消息的问题。

📄 原文内容

Filters the name to associate with the “from” email address.

Parameters

$from_namestring
Name associated with the “from” email address.

More Information

  • The filter modifies the “from name” used in an email sent using the wp_mail() function. When used together with the ‘wp_mail_from‘ filter, it creates a from address like “Name ”. The filter should return a string.
  • If you apply your filter using an anonymous function, you cannot remove it using remove_filter() .

Source

$from_name = apply_filters( 'wp_mail_from_name', $from_name );

Changelog

Version Description
2.3.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Example migrated from Codex:

    add_filter( 'wp_mail_from_name', 'custom_wp_mail_from_name' );
    function custom_wp_mail_from_name( $original_email_from ) {
    	return 'WordPress Email System';
    }

    It is not necessary to call another method if you can use anonymous functions (PHP 5.3.0+):

    add_filter( 'wp_mail_from_name', function( $name ) {
    	return 'WordPress Email System';
    } );

  2. Skip to note 4 content

    For subdomains why do we have to add:

    add_filter( 'wp_mail_from', function ( $email ) {
        return 'noreply@example.com';
    } );
    
    add_filter( 'wp_mail_from_name', function ( $name ) {
        return 'Sender Name';
    } );

    I didn’t know that international customers couldn’t send me messages for over a year T-T CF7 forms would just have a spinning mark. My German friend told me this. It’s for all subdomain sites not the main site. I’m glad I found this out now though.