将自定义字段值发送到电子邮件
Send custom field value to email
我创建了名为 Emails 的自定义 post 类型,并使用高级自定义字段插件向自定义 post 类型中的一个 post 添加了一个自定义字段,名为电子邮件页脚,该字段是应该显示在从网站发出的每封自动电子邮件底部的图像字段。
我正在使用的当前代码
function wpcf7ev_verify_email_address2( $wpcf7_form ){
$email_footer = '<html>
<body style="color:#000000;">
<div style="font-size:16px;font-weight:bold;margin-top:20px;">
Regards,
<br/>
$email_footer .= '<img src="http://mysite.col/footer_image.jpg" width="100%" alt=""/>
</div>';
$email_footer .='<div style="display:none;">'.generateRandomString().
'</div></body>
</html>
';
代码正在运行,它在底部显示带有此 url 的图像:http://mysite.col/footer_image.jpg
但我不想硬编码,我希望能够使用我创建的自定义字段对其进行修改
我查看了 ACF 文档并找到了这个,但我不知道如何使用它在我创建的自定义 post 类型上仍然显示该确切字段:
<?php
$image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
您从 ACF 文档中概述的代码告诉您如何使用图像(具有类型数组)从 ACF 字段获取图像。
如果我们要将此实现到您的函数中,我们将不得不从页面的某处引用图像。在不知道如何称呼它的情况下,您可以通过多种方式嵌入它。
第一种方式,我们将它传递给页面上调用的函数,就像这样...
wpcf7ev_verify_email_address2(get_field('image'));
然后像这样更新你的函数...
function wpcf7ev_verify_email_address2($image, $wpcf7_form)
{
$email_footer = '<div style="font-size:16px;font-weight:bold;margin-top:20px;">Regards,<br/>';
// get the image from the passed in image function.
$email_footer .= '<img src="' . $image['url'] . '" width="100%" alt="' . $image['alt'] . '"/></div>';
$email_footer .='<div style="display:none;">' . generateRandomString() . '</div>';
}
或者,第二种方式,如果您正在调用函数来修改操作或其他内容,则必须从 ACVF 设置中分配给它的任何页面 ID/选项页面获取图像。这将使您的函数看起来有点像这样:
function wpcf7ev_verify_email_address2($wpcf7_form)
{
// get image acf field from page with id 1
$image = get_field('image', 1);
// or get image from acf field on options page
// $image = get_field('image', 'options');
$email_footer = '<div style="font-size:16px;font-weight:bold;margin-top:20px;">Regards,<br/>';
$email_footer .= '<img src="' . $image['url'] . '" width="100%" alt="' . $image['alt'] . '"/></div>';
$email_footer .='<div style="display:none;">' . generateRandomString() . '</div>';
}
以上所有假设您的功能都按预期工作,您需要帮助获取 ACF 字段,并且图像已上传。如果需要,您可以将 get_field 的声明包装在 if
语句中。
我创建了名为 Emails 的自定义 post 类型,并使用高级自定义字段插件向自定义 post 类型中的一个 post 添加了一个自定义字段,名为电子邮件页脚,该字段是应该显示在从网站发出的每封自动电子邮件底部的图像字段。
我正在使用的当前代码
function wpcf7ev_verify_email_address2( $wpcf7_form ){
$email_footer = '<html>
<body style="color:#000000;">
<div style="font-size:16px;font-weight:bold;margin-top:20px;">
Regards,
<br/>
$email_footer .= '<img src="http://mysite.col/footer_image.jpg" width="100%" alt=""/>
</div>';
$email_footer .='<div style="display:none;">'.generateRandomString().
'</div></body>
</html>
';
代码正在运行,它在底部显示带有此 url 的图像:http://mysite.col/footer_image.jpg
但我不想硬编码,我希望能够使用我创建的自定义字段对其进行修改
我查看了 ACF 文档并找到了这个,但我不知道如何使用它在我创建的自定义 post 类型上仍然显示该确切字段:
<?php
$image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
您从 ACF 文档中概述的代码告诉您如何使用图像(具有类型数组)从 ACF 字段获取图像。
如果我们要将此实现到您的函数中,我们将不得不从页面的某处引用图像。在不知道如何称呼它的情况下,您可以通过多种方式嵌入它。
第一种方式,我们将它传递给页面上调用的函数,就像这样...
wpcf7ev_verify_email_address2(get_field('image'));
然后像这样更新你的函数...
function wpcf7ev_verify_email_address2($image, $wpcf7_form)
{
$email_footer = '<div style="font-size:16px;font-weight:bold;margin-top:20px;">Regards,<br/>';
// get the image from the passed in image function.
$email_footer .= '<img src="' . $image['url'] . '" width="100%" alt="' . $image['alt'] . '"/></div>';
$email_footer .='<div style="display:none;">' . generateRandomString() . '</div>';
}
或者,第二种方式,如果您正在调用函数来修改操作或其他内容,则必须从 ACVF 设置中分配给它的任何页面 ID/选项页面获取图像。这将使您的函数看起来有点像这样:
function wpcf7ev_verify_email_address2($wpcf7_form)
{
// get image acf field from page with id 1
$image = get_field('image', 1);
// or get image from acf field on options page
// $image = get_field('image', 'options');
$email_footer = '<div style="font-size:16px;font-weight:bold;margin-top:20px;">Regards,<br/>';
$email_footer .= '<img src="' . $image['url'] . '" width="100%" alt="' . $image['alt'] . '"/></div>';
$email_footer .='<div style="display:none;">' . generateRandomString() . '</div>';
}
以上所有假设您的功能都按预期工作,您需要帮助获取 ACF 字段,并且图像已上传。如果需要,您可以将 get_field 的声明包装在 if
语句中。