如何使用过滤器动态短码?

时间:2014-11-17 04:57:18

标签: wordpress filter shortcode

我只是创建这样的短代码:

add_shortcode( 'demo1', 'demo1_init' ) );
add_shortcode( 'demo2', 'demo2_init' ) );

function demo1_init() {
   // Shortcode Def. here...
}

function demo2_init() {
   // Shortcode Def. here...
}

如何使用过滤器动态创建这些短代码?像这样的东西。 E.g。

 $a = array(
        "demo1"       =>  "demo1_init",
        "demo2"       =>  "demo2_init",
);

1 个答案:

答案 0 :(得分:0)

以下内容应该完成工作

function demo1_init() {
    // Shortcode Def. here...
    return "demo1";
}

function demo2_init() {
    // Shortcode Def. here...
    return "demo2";
}

function register_shortcodes() {

    $shortcodes = array(
        "demo1"   =>  "demo1_init",
        "demo2"   =>  "demo2_init",
    );

    foreach($shortcodes as $shortcode => $func) {       
        add_shortcode($shortcode, $func);
    }

}
add_action( "init", "register_shortcodes" );