从模板自定义 WordPress 新用户电子邮件

Custom WordPress New User E-Mail from Template

从一个注册用户的自定义函数,我插入这个函数来生成和覆盖新用户通知的 WordPress 系统电子邮件。

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
      global $wpcargo;
        $user_url = stripslashes( $user->user_url );
        $user_login = stripslashes( $user->user_login );
        $user_email = stripslashes( $user->user_email );
        $user_firstname = stripslashes( $user->user_firstname );
        $user_last_name = stripslashes( $user->user_last_name );
        $user_pass = stripslashes( $user->user_pass );
        $message = file_get_contents('../email/mail-template.php');
        $wp_new_user_notification_email['subject'] = sprintf( '[%s] Welcome.', $blogname );
        $wp_new_user_notification_email['headers'] = array('Content-Type: text/html; charset=UTF-8');
        $wp_new_user_notification_email['message'] = $message; 
        return $wp_new_user_notification_email;
    }
    add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );

正在发送电子邮件,但我无法在 PHP / HTML 文件中打印用户数据。

<p style="margin-top:0;margin-bottom:12px;"><b>Name</b>: <?php $user_firstname; ?></p>

我的代码有误,有什么建议吗?

我会使用 locate_template 作为电子邮件内容,使用 output buffering 而不是 file_get_contents - 正如@Zak 指出的那样,你必须 echo 你的输出否则它不会出现。

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
    global $wpcargo;
    $user_url = stripslashes( $user->user_url );
    $user_login = stripslashes( $user->user_login );
    $user_email = stripslashes( $user->user_email );
    $user_firstname = stripslashes( $user->user_firstname );
    $user_last_name = stripslashes( $user->user_last_name );
    $user_pass = stripslashes( $user->user_pass );
    ob_start();
    include( locate_template( '/email/mail-template.php' ); // This path may vary depending on your setup.
    $wp_new_user_notification_email['message'] = ob_get_clean();
    $wp_new_user_notification_email['subject'] = sprintf( '[%s] Welcome.', $blogname );
    $wp_new_user_notification_email['headers'] = array('Content-Type: text/html; charset=UTF-8');
    return $wp_new_user_notification_email;
}
add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );

在您的模板中,您要确保对正在输出的任何内容进行回显和 esc_html()

<?php echo esc_html( $user_firstname ); ?>