具有多个选项作为属性的表单快捷代码

时间:2017-12-03 作者:Iurie

我有一个大的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\');

1 个回复
最合适的回答,由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\');
请注意.= 符号

结束

相关推荐

Shortcode IF statment help

下面是我一直致力于运行短代码的代码。我已经尝试实施if() 声明,但没有任何效果。我有两个输出。我想做的是,如果属性(\'img\', \'user\', \'text\') 显示在快捷码中$output1.否则,如果属性(\'img\', \'text\', \'length\', \'material\', \'power\') 显示在快捷码中$output2.function hire_equipment_func($atts) { extract( s