我经常做输出缓冲,这对我很有用。让我们看看这个例子。未测试。
和esc_attr() 将处理各种用户输入错误的转义。
<?php
/**
 * Container shortcode
 *
 * Outputs a container wrapping the content.
 * 
 * @param  array $atts     Array of shortcode attributes.
 * @param  string $content Content within a shortcode.
 * @return string          The formatted content.
 */
function wpse_228875_shortcode( $atts, $content ) {
    $atts = shortcode_atts( array(
                \'type\'          => \'\',
                \'margintop\'     => \'\',
                \'marginbottom\'  => \'\',
            ), $atts, \'container\' );
    $type           = isset($atts[\'type\'])          ? $atts[\'type\']         : \'\';
    $margintop      = isset($atts[\'margintop\'])     ? $atts[\'margintop\']    : \'\';
    $marginbottom   = isset($atts[\'marginbottom\'])  ? $atts[\'marginbottom\'] : \'\';
    //start output buffering
    ob_start();
    ?>
    <div class="container <?php echo esc_attr($type); ?>" style="margin-top:<?php echo esc_attr($margintop); ?>; margin-bottom:<?php echo esc_attr($marginbottom); ?>;">
        <section>
            <div class="row">
                <?php
                /**
                 * Using do_shortcode() will allow you
                 * to use another shortcode within the
                 * container shortcode.
                 */
                echo do_shortcode( apply_filters( \'the_content\', $content ) );
                ?>
            </div> <!-- /.row -->
        </section>
    </div> <!-- /.container -->
    <?php
    //cleanup the buffering and return everything within
    return ob_get_clean();
}
add_shortcode( \'container\', \'wpse_228875_shortcode\', 10, 2 );
 脚注:什么是输出缓冲没有输出缓冲(默认设置),当PHP通过脚本处理时,HTML会被分块发送到浏览器。通过输出缓冲,HTML存储在一个变量中,并作为脚本末尾的一部分发送到浏览器
资料来源:https://stackoverflow.com/a/2832179/1743124