钩子文档

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.

  • to string|string[]
    Array or comma-separated list of email addresses to send message.
  • subject string
    Email subject.
  • message string
    Message contents.
  • headers string|string[]
    Additional headers.
  • attachments string|string[]
    Paths to files to attach.
  • embeds string|string[]
    Paths to files to embed.

More Information

  • The wp_mail filter hook allows you to filter the arguments that are passed to the wp_mail() function. The arguments for wp_mail() are passed through the filter as an array.
  • $attachments should be an array. If it is not, it will be converted to one by the wp_mail function after the filter.

Source

$atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments', 'embeds' ) );

Changelog

Version Description
2.2.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    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;
      }