[英]Add custom field inside a do_shortcode
I'm trying to add a contact form into a page template with contact form 7. However, I want the destination email to come in from a custom field I have created.我正在尝试将联系表格添加到带有联系表格 7 的页面模板中。但是,我希望目标电子邮件来自我创建的自定义字段。
The code that pulls the custom field data in is this:拉取自定义字段数据的代码是这样的:
<?php the_field('website_address'); ?>
The shortcode which lets me select the destination email at template level is:让我在模板级别选择目标电子邮件的简码是:
<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email="xxxxxx@example.com"]'); ?>
So I need to add the website_address into the shortcode.所以我需要将 website_address 添加到短代码中。 I've tried the following:
我尝试了以下方法:
<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email="<?php the_field('website_address'); ?>"]'); ?>
But it has a PHP tag inside a PHP tag so it doesn't work...但是它在 PHP 标签中有一个 PHP 标签,所以它不起作用......
So I tried these:所以我尝试了这些:
<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email="the_field('website_address')"]'); ?>
<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email="get_field('website_address')"]'); ?>
<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email=".get_field('website_address')."]'); ?>
<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email="'.get_field('website_address').'"]'); ?>
None of which are working correctly, some even break the page.没有一个工作正常,有些甚至打破了页面。
I'm struggling to find answer so was wondering if someone could enlighten me - I might even be thinking of it all in the wrong way!我正在努力寻找答案,所以想知道是否有人可以启发我 - 我什至可能以错误的方式思考这一切! Thanks a lot for looking :)
非常感谢您的关注:)
EDIT编辑
There's a function I need to include which I have added to functions.php but it still isn't helping...有一个我需要添加到functions.php中的函数,但它仍然没有帮助......
add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 );
function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
$my_attr = 'destination-email';
if ( isset( $atts[$my_attr] ) ) {
$out[$my_attr] = $atts[$my_attr];
}
return $out;
}
It's from the page: https://contactform7.com/getting-default-values-from-shortcode-attributes/它来自页面: https : //contactform7.com/getting-default-values-from-shortcode-attributes/
Ok, I found a way to do this as explained here: https://wordpress.org/support/topic/getting-emaip-recipient-from-shortcode/好的,我找到了一种方法,如下所述: https : //wordpress.org/support/topic/getting-emaip-recipient-from-shortcode/
Add the shortcode with the destination email in the page or post where you want to show the form:在页面中添加带有目标电子邮件的短代码或发布您要显示表单的位置:
[contact-form-7 id="123" title="Contact Form" destination-email="xxxxxx@example.com"] [contact-form-7 id="123" title="联系表格" destination-email="xxxxxx@example.com"]
Add the following code to the theme functions.php or your custom plugin.将以下代码添加到主题 functions.php 或您的自定义插件中。
add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 ); add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3); function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) { $my_attr = 'destination-email';
function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) { $my_attr = 'destination-email'; if ( isset( $atts[$my_attr] ) ) { $out[$my_attr] = $atts[$my_attr];
if ( isset( $atts[$my_attr] ) ) { $out[$my_attr] = $atts[$my_attr]; } return $out;
} 返回 $out;
} 3. Add this code to the Form Tab: 3. 将此代码添加到表单选项卡:
[email* destination-email default:shortcode_attr]
//If you want this field hidden you can use this code instead: //如果你想隐藏这个字段,你可以使用这个代码:
[hidden destination-email default:shortcode_attr]
If you used the hidden field it will show the “Invalid mailbox syntax” error, but in my case I can ignore it because it works.如果您使用隐藏字段,它将显示“无效的邮箱语法”错误,但在我的情况下,我可以忽略它,因为它有效。
If the To field with hidden field doesn't work, you can add the id to the email shorcode:如果带有隐藏字段的收件人字段不起作用,您可以将 id 添加到电子邮件 shorcode:
[email* destination-email default:shortcode_attr id:hidefield]
And hide the field with css:并用 css 隐藏该字段:
#hidefield {
display: none;
}
Then I just used this:然后我就用了这个:
<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email="'.get_field('email_address').'"]'); ?>
to add in the email address from the custom post type :)从自定义帖子类型添加电子邮件地址:)
Thanks everyone who answered!感谢所有回答的人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.