wp_mail
云策文档标注
概述
本文档介绍了 wp_mail 过滤器钩子,用于过滤传递给 wp_mail() 函数的参数数组。开发者可以通过此钩子修改邮件发送的收件人、主题、内容等参数。
关键要点
- wp_mail 过滤器允许过滤 wp_mail() 函数的参数,参数以数组形式传递。
- 参数包括 to(收件人)、subject(主题)、message(内容)、headers(头部)、attachments(附件)和 embeds(嵌入文件)。
- $attachments 参数应为数组,如果不是,wp_mail 函数会在过滤器后自动转换。
代码示例
add_filter('wp_mail','redirect_mails', 10,1);
function redirect_mails($args){
$to = $args['to'];
//$args['subject']
//$args['message']
//$args['headers']
//$args['attachments']
$user = get_user_by( 'email', $to);
$_role = get_user_meta($user->ID, 'my_custom_role', true);
if ($role == 'opportunity-owner') {
$test_mentor_email = get_option('test_mentor_email');
if ($test_mentor_email != '') {
$to = $test_mentor_email;
}
}
$args['to']=$to;
return $args;
}
原文内容
Filters the wp_mail() arguments.
Parameters
$argsarray-
Array of the
wp_mail()arguments.tostring|string[]Array or comma-separated list of email addresses to send message.subjectstringEmail subject.messagestringMessage contents.headersstring|string[]Additional headers.attachmentsstring|string[]Paths to files to attach.embedsstring|string[]Paths to files to embed.
Source
$atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments', 'embeds' ) );
Changelog
| Version | Description |
|---|---|
| 2.2.0 | Introduced. |
Skip to note 2 content
Aurovrata Venet
modify the recipient of the email
add_filter('wp_mail','redirect_mails', 10,1); function redirect_mails($args){ $to = $args['to']; //$args['subject'] //$args['message'] //$args['headers'] //$args['attachments'] $user = get_user_by( 'email', $to); $_role = get_user_meta($user->ID, 'my_custom_role', true); if ($role == 'opportunity-owner') { $test_mentor_email = get_option('test_mentor_email'); if ($test_mentor_email != '') { $to = $test_mentor_email; } } $args['to']=$to; return $args; }