admin管理员组文章数量:1394189
I'm building a method for my client to construct a message content to be sent with wp_mail. Within that I'd like them to be able to choose variables, for example the message might look like this in my admin custom field:
Email: $email_address$
First Name: $first_name$
So that when I iterate over the lines I can spot a variable place-holder and replace $email_address$ with my variable so my PHP would look like:
$body = 'Email Address: [email protected]<br>First Name: Mickey Mouse';
wp_mail( $to, $subject, $body, $headers );
I suppose I'm wondering if there's a particular or PHP-ish way to do this, I'm sure I've seen it in plugins before but don't recall the exact formatting.
I'm building a method for my client to construct a message content to be sent with wp_mail. Within that I'd like them to be able to choose variables, for example the message might look like this in my admin custom field:
Email: $email_address$
First Name: $first_name$
So that when I iterate over the lines I can spot a variable place-holder and replace $email_address$ with my variable so my PHP would look like:
$body = 'Email Address: [email protected]<br>First Name: Mickey Mouse';
wp_mail( $to, $subject, $body, $headers );
I suppose I'm wondering if there's a particular or PHP-ish way to do this, I'm sure I've seen it in plugins before but don't recall the exact formatting.
Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Feb 6, 2020 at 11:31 Kevin NugentKevin Nugent 5631 gold badge8 silver badges20 bronze badges1 Answer
Reset to default 1You should use str_replace, which accepts arrays as arguments:
$content = "Email: {{email}}\nFirst Name: {{first_name}}";
$body = str_replace(
['{{email}}', '{{first_name}}'],
['[email protected]', 'Mickey Mouse'],
$content
);
Be careful about using $
in your placeholders. Since it's widely used in PHP, it's not safe. It was the reason I've used mustache-like syntax in my example.
本文标签: Pass Variables or Variable PlaceHolder from Editor to PHP
版权声明:本文标题:Pass Variables or Variable Place-Holder from Editor to PHP 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744772780a2624440.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论