我有一个大的MailChimp订阅表单,可以选择订阅多个/单独的类别,由一个快捷码函数返回。表单和短代码都可以,但现在我希望每个表单类别选项都作为一个单独的属性生成/返回,以便能够在具有不同属性/选项的不同位置使用此表单短代码。例如:在订阅页面中,我将显示包含所有选项的完整表单,但在类别页面的末尾或每个帖子的末尾,我将显示包含仅订阅该帖子类别选项的表单。
我知道如何用属性创建一个快捷码函数,有很多这样做的例子,但我不知道如何在表单中使用属性,表单本身是作为变量返回的。有什么帮助吗?
P、 S.每个类别选项都有适当的选项,每日和每周,但它们是类别选项的一部分,因此无需将它们作为单独的属性/参数创建/返回。
我的shortcode函数示例:
// The shortcode function
function mailchimp_form_shortcode( $atts = [], $content = null ) {
$mc_form=\'
<div id="mc_embed_signup">
<form action="xxxxxxx" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form">
<div id="mc_embed_signup_scroll">
<div class="container">
<div class="mc-field-group">
<label for="mce-EMAIL">E-mail: <span class="asterisk"> (required)</span></label>
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="e-mail">
</div>
<!-- Subscribe options -->
<p class="subscribe-options"> - Category 1 <!-- Subscribe option 1 -->
<span>
<input type="checkbox" value="1234" name="group[1111][1234]" id="mce-group[1111]-1111-0"> daily<span class="asterisk">*</span>
<input type="checkbox" value="1235" name="group[1111][1235]" id="mce-group[1111]-1111-1"> weekly
</span>
</p>
<p class="subscribe-options"> - Category 2 <!-- Subscribe option 2 -->
<span>
<input type="checkbox" value="1236" name="group[1111][1236]" id="mce-group[1111]-1111-2"> daily<span class="asterisk">*</span>
<input type="checkbox" value="1237" name="group[1111][1237]" id="mce-group[1111]-1111-3"> weekly
</span>
</p>
</div> <!-- .container -->
<div id="mce-responses" class="clear">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div> <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
<div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="xxxxxxx" tabindex="-1" value=""></div>
<div class="clear">
<input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button">
</div>
</div>
</form>
</div>\';
return $mc_form;
}
add_shortcode(\'mailchimp\', \'mailchimp_form_shortcode\');
最合适的回答,由SO网友:kierzniak 整理而成
可以通过以下方式使用快捷码属性:
// The shortcode function
function mailchimp_form_shortcode( $atts = [], $content = null ) {
$attributes = shortcode_atts( array(
\'option1\' => \'no\',
\'option2\' => \'no\',
), $atts );
$mc_form = \'
<div id="mc_embed_signup">
<form action="xxxxxxx" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form">
<div id="mc_embed_signup_scroll">
<div class="container">
<div class="mc-field-group">
<label for="mce-EMAIL">E-mail: <span class="asterisk"> (required)</span></label>
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="e-mail">
</div>\';
if( $attributes[\'option1\'] === \'yes\' ) {
$mc_form .= \'
<!-- Subscribe options -->
<p class="subscribe-options"> - Category 1 <!-- Subscribe option 1 -->
<span>
<input type="checkbox" value="1234" name="group[1111][1234]" id="mce-group[1111]-1111-0"> daily<span class="asterisk">*</span>
<input type="checkbox" value="1235" name="group[1111][1235]" id="mce-group[1111]-1111-1"> weekly
</span>
</p>\';
}
if( $attributes[\'option2\'] === \'yes\' ) {
$mc_form .= \'
<p class="subscribe-options"> - Category 2 <!-- Subscribe option 2 -->
<span>
<input type="checkbox" value="1236" name="group[1111][1236]" id="mce-group[1111]-1111-2"> daily<span class="asterisk">*</span>
<input type="checkbox" value="1237" name="group[1111][1237]" id="mce-group[1111]-1111-3"> weekly
</span>
</p>\';
}
$mc_form .= \'
</div> <!-- .container -->
<div id="mce-responses" class="clear">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div> <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
<div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="xxxxxxx" tabindex="-1" value=""></div>
<div class="clear">
<input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button">
</div>
</div>
</form>
</div>\';
return $mc_form;
}
add_shortcode(\'mailchimp\', \'mailchimp_form_shortcode\');
请注意
.=
符号