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 user145078user1450781 Answer
Reset to default 0I 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).