允许用户在wp_mail()中添加php代码

时间:2018-12-17 作者:Alt C

我有一封邮件被触发,如下所示。我希望用户能够自定义内容。

我想让用户能够esc_html($user->display_name) 不知何故,可能会有这样的文字{{user_name}}

$body = sprintf(\'Hey %s, your awesome post has been published!
         See <%s>\',
                 esc_html($user->display_name),
                 get_permalink($post)
         );

    // Now send to the post author.
    wp_mail($user->user_email, \'Your post has been published!\', $body);
有可能吗?

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

我会用str_replace() 处理“伪”短代码。类似这样:

$body = sprintf(\'Hey {{username}}, your awesome post has been published!
    See <%s>\', get_permalink($post) );

// Add replacement values to the array as necessary.
$old = array( \'{{username}}\' );
$new = array( esc_html($user->display_name) );

$body = str_replace( $old, $new, $body );

// Now send to the post author.
wp_mail($user->user_email, \'Your post has been published!\', $body);
我只是从你开始的工作开始。我想你应该在这之前做点什么$user 对象此外,我还为str_replace() 作为数组,因此可以根据需要添加到其中(而不是多次运行)。

相关推荐