使用添加短码和回调动态创建短码

时间:2015-07-31 作者:piggypig

在下面的示例中,$shortcodes 是包含短代码数据的多维数组。

foreach( $shortcodes as $shortcode ) {

    // Here we have the shortcode name, could be \'custom_person_john\'.
    $shortcode_name = $shortcode[\'slug\'];

    // Anonymous function should be referred to as \'custom_person_john\'.
    $shortcode_name = function( $shortcode ) {

      // Build shortcode attributes.
      $atts = shortcode_atts( array(
        \'name\' => \'john\',
        \'type\' => \'mammal\'
      ), $atts );

      $markup = \'<div class="\'.$atts[\'name\'].\'">\';
         $markup .= \'<div>\';
            // $shortcode should contain custom markup from the array.
            $markup .= $shortcode[\'html\'];
         $markup .= \'</div>\';
      $markup .= \'</div>\';

      return $markup;

    };

    // Trying to register the shortcode
    add_shortcode( $shortcode[\'slug\'], $shortcode_name( $shortcode ) );

};
所以这不起作用,但我不明白为什么。如果我转储所有注册的短代码,它们不会显示在列表中。正在尝试加载simple echo的[custom\\u person\\u john]。我也没有错误。

编辑:如果有人对如何从数组中动态创建它们有其他想法,请随时提出建议:)

2 个回复
最合适的回答,由SO网友:mmm 整理而成

短代码在列表中不起作用,因为它不是在第二个参数上具有有效函数回调的有效声明

尝试此操作以查看附加内容:

// shortcode data

add_filter("shortcode_list", function ($shortcode_list) {
    $shortcode_list["slugA"] = array(
        "html" => "test html 1",
    );

    return $shortcode_list;
});

add_filter("shortcode_list", function ($shortcode_list) {
    $shortcode_list["slugB"] = array(
        "html" => "test <strong>html 2 - [slugA]</strong>",
    );

    return $shortcode_list;
});


// shortcodes declaration

add_action("init", function () {

    $shortcodes = apply_filters("shortcode_list", array());

    foreach (array_keys($shortcodes) as $shortcode_slug) {
        add_shortcode($shortcode_slug, "shortcode_callback");
    };

});


// callback function

function shortcode_callback($atts, $content = "", $tag) {

    // Build shortcode attributes.
    $atts = shortcode_atts( array(
        \'name\' => \'john\',
        \'type\' => \'mammal\'
    ), $atts);


    // shortcode data
    $shortcodes = apply_filters("shortcode_list", array());
    $shortcode = $shortcodes[$tag];


    // construct result of the shortcode

    $markup  = "";
    $markup .= \'<div class="\'.$atts[\'name\'].\'">\';
        $markup .= \'<div>\';
            // $shortcode should contain custom markup from the array.
            $markup .= $shortcode[\'html\'];
            //$markup .= do_shortcode($shortcode[\'html\']); // to replace shortcodes in the "html" data


            $markup .= \'<pre>\';
            $markup .= print_r($atts, TRUE);
            $markup .= \'</pre>\';

        $markup .= \'</div>\';
    $markup .= \'</div>\';

    return $markup;

}

SO网友:Mohammad Mursaleen

几个月前,我在做一件类似的事情short codes 动态。但我采取了不同的方法。

我所做的如下:;

我注册了short codecustom post type 为此我必须使用动态short code.

然后添加attribute 对于post name (slug) 因为short code 动态获取数据以显示在前端。

以下是如何做到这一点的示例代码;

// Shortcode to display html in frontend
// [dynamic_shortcode name=\'post-slug\']
function dynamic_shortcode( $atts ) {

    // Get Attributes
    extract( shortcode_atts(
            array(
                \'name\' => \'\'   // DEFAULT SLUG SET TO EMPTY
            ), $atts )
    );

    // WP_Query arguments
    $args = array (
        \'name\'              =>  $name,     // GET POST BY SLUG  // IGNORE IF YOU ARE GETTING ERROR ON THIS LINE IN YOUR EDITOR
        \'post_type\'         => \'your-post-type\', // YOUR POST TYPE
        \'post_status\'       => \'publish\', // POST STATUS
    );


    // The Query
    $query = new WP_Query( $args );

    // The Loop
    if ( $query->have_posts() && $name != \'\' ) {
        while ( $query->have_posts() ) {
            $query->the_post();

            $html_src = get_post_meta( get_the_ID() , \'html_src\' , true);  // GET HTML SOURCE FROM YOUR POST META

            if ( $html_src) {
                return $html_src; // RETURN HTML TO DISPLAY ON FRONT END
            }

        }
    } else {
        // no posts found
        return \'<p style="color: red" >Sorry You have set no html for this slug...</p>\';

    }


// Restore original Post Data
    wp_reset_postdata();
}
add_shortcode( \'dynamic_shortcode\', \'dynamic_shortcode\' );
希望这对你也有用。

结束

相关推荐

SHORTCODE_UNAUTOP()是否损坏?

shortcode_unautop() 在里面/wp-includes/formatting.php 应该在文本块中找到短代码,并从中删除换行段落标记。在这个过程中,我一直对段落标记存在问题。以下是var_dump($pee), 我将其放在函数的最开头,即处理前的字符串:string(353) \"<p>[row wrap=\"true\"]</p> <p>[one_half]</p> <p>[text_block]Fusce