最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Allow user to add the php code in wp_mail()

programmeradmin3浏览0评论

I have an email triggered like below. I want a user to be able to customize the content.

I want to provide the user to be able to put esc_html($user->display_name) somehow may some thing with a text like {{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);

Is it possible to do?

I have an email triggered like below. I want a user to be able to customize the content.

I want to provide the user to be able to put esc_html($user->display_name) somehow may some thing with a text like {{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);

Is it possible to do?

Share Improve this question edited Dec 17, 2018 at 21:30 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Dec 17, 2018 at 21:06 user145078user145078
Add a comment  | 

1 Answer 1

Reset to default 0

I would just use str_replace() to handle a "pseudo" shortcode. Something like this:

$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);

I just worked from what you started with. I'm assuming you're doing something ahead of this to get the $user object. Also, I made the replacement values for str_replace() as arrays, so you can add to that as necessary (instead of running it multiple times).

发布评论

评论列表(0)

  1. 暂无评论