向多个电子邮件地址发送管理员电子邮件

时间:2014-12-18 作者:Pat Gilmour

是否有一个钩子允许我通过电子邮件发送多个默认管理电子邮件通知的电子邮件地址?

我希望我可以构建一个阵列:

$adminEmails = array(\'[email protected]\', \'[email protected]\');
然后将所有管理电子邮件(如新用户通知)发送到$adminEmails

可能的

3 个回复
最合适的回答,由SO网友:shanebp 整理而成

尝试以下操作:

update_option( \'admin_email\', \'[email protected], [email protected]\' );
请注意,该值是一个字符串;仅打开和关闭报价!

SO网友:sMyles

这可以通过过滤wp_mail 函数,检查是否to 设置为管理电子邮件,如果是,请添加其他电子邮件地址,并将参数返回到wp_mail

add_filter( \'wp_mail\', \'my_custom_to_admin_emails\' );

/**
 * Filter WP_Mail Function to Add Multiple Admin Emails
 *
 *
 *
 * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
 *                    subject, message, headers, and attachments values.
 *
 * @return array
 */
function my_custom_to_admin_emails( $args ) {
    // If to isn\'t set (who knows why it wouldn\'t) return args
    if( ! isset($args[\'to\']) || empty($args[\'to\']) ) return $args;

    // If TO is an array of emails, means it\'s probably not an admin email
    if( is_array( $args[\'to\'] ) ) return $args;

    $admin_email = get_option( \'admin_email\' );

    // Check if admin email found in string, as TO could be formatted like \'Administrator <[email protected]>\',
    // and if we specifically check if it\'s just the email, we may miss some admin emails.
    if( strpos( $args[\'to\'], $admin_email ) !== FALSE ){
        // Set the TO array key equal to the existing admin email, plus any additional emails
        //
        // All email addresses supplied to wp_mail() as the $to parameter must comply with RFC 2822. Some valid examples:
        // [email protected]
        // User <[email protected]>
        $args[\'to\'] = array( $args[\'to\'], \'[email protected]\', \'Admin4 <[email protected]>\' );
    }

    return $args;
}
我们将TO作为数组返回,如下所示wp_mail 将处理阵列并根据需要将其分解以发送电子邮件

SO网友:Nimrod

这是我的解决方案,它使用update\\u option\\u*过滤器,我相信这是正确的方法。将其添加到插件或主题函数中。php文件,然后您可以在设置->常规屏幕中安全地放置逗号分隔的管理电子邮件。

add_filter(\'pre_update_option_admin_email\',\'sanitize_multiple_emails\',10,2);

function sanitize_multiple_emails($value,$oldValue)
{
    //if anything is fishy, just trust wp to keep on as it would.
    if(!isset($_POST["admin_email"]))
        return $value;

    $result = "";
    $emails = explode(",",$_POST["admin_email"]);
    foreach($emails as $email)
    {
        $email = trim($email);
        $email = sanitize_email( $email );

        //again, something wrong? let wp keep at it.
        if(!is_email($email))
            return $value;
        $result .= $email.",";

    }

    if(strlen($result == ""))
        return $value;
    $result = substr($result,0,-1);

    return $result;
}

结束

相关推荐

Admin_head-post.php仅在发布/更新后才起作用

我用一些管理后端创建了一个自定义帖子类型。目前我正在使用钩子调用脚本admin_head-post.php 但这似乎只有在自定义帖子类型创建(发布时)或更新后才会触发。在特定的管理页面上运行的更好的钩子是什么,但在最初创建新帖子以及更新/发布等时又是什么?