Php 使用数组自动生成wordpress短代码?

Php 使用数组自动生成wordpress短代码?,php,arrays,wordpress,for-loop,shortcode,Php,Arrays,Wordpress,For Loop,Shortcode,我正在创建一个短代码,它自动生成具有给定数组键和值的短代码。函数名不会动态生成 注意:数组键=ShortcodeName,值=Wordpress选项字段 add_shortcode("auto_gen", "auto_gen"); function auto_gen() { $a = array( "get_address" => "mg_admin_address", "get_phone" => "mg_ad

我正在创建一个短代码,它自动生成具有给定数组键和值的短代码。函数名不会动态生成

注意:数组键=ShortcodeName,值=Wordpress选项字段

add_shortcode("auto_gen", "auto_gen");
function auto_gen() {
    $a = array(
        "get_address"       =>  "mg_admin_address",
        "get_phone"         =>  "mg_admin_phone",
        "get_fax"           =>  "mg_admin_fax",
        "get_email"         =>  "mg_admin_email",
        "get_hrs_mon"       =>  "mg_work_hrs_mon_frd",
        "get_hrs_sat"       =>  "mg_work_hrs_sat"
    );
    foreach ($a as $k => $v) {
        if(has_shortcode($k)) {
            echo "<br>Found: ". $k;
        } else {
            add_shortcode($k,  $k. "_init");
            function $k. "_init"() {
                return get_option[$v, ''];
            }
        }
        add_shortcode();
        echo $k ." -> ". $v. "<br />";
    }
}
添加快捷码(“自动生成”、“自动生成”);
函数auto_gen(){
$a=数组(
“获取地址”=>“mg\U管理地址”,
“get_phone”=>“mg_admin_phone”,
“获取传真”=>“mg管理传真”,
“获取电子邮件”=>“mg\U管理电子邮件”,
“每天工作时间”=>“每天工作时间”,
“获取小时数”=>“mg工作小时数”
);
foreach($a为$k=>$v){
if(有_短码($k)){
回声“
发现:”.$k; }否则{ 添加快捷码($k,$k.“_init”); 函数$k.“_init”(){ 返回get_选项[$v',]; } } 添加_shortcode(); 回声$k.“->”$v.“
”; } }
有任何可能的方法可以做到这一点

注意:

这里,get_地址数组键是一个短码。它是在通过循环时动态生成的。get_地址可更改。如果我用get_user_address更改get_address,则生成get_user_address。“get_地址”、“get_电话”可在最终级别更改

开发人员还可以使用get_选项生成快捷码来访问创建的wp_选项,只需在数组中推送元素。e、 g.“快捷代码名称”=>“选项名称”

请尝试以下代码:

add_shortcode("auto_gen", "auto_gen");
    function auto_gen() {
        $a = array(
            "get_address"       =>  "mg_admin_address",
            "get_phone"         =>  "mg_admin_phone",
            "get_fax"           =>  "mg_admin_fax",
            "get_email"         =>  "mg_admin_email",
            "get_hrs_mon"       =>  "mg_work_hrs_mon_frd",
            "get_hrs_sat"       =>  "mg_work_hrs_sat"
        );
        foreach ($a as $k => $v) {
            if(has_shortcode($v,$k)) {
                echo "<br>Found: ". $k;
            } else {
                add_shortcode($k,  $k."_init");
                $func =$k."_init";
        $func($v); 
            }
            echo $k ." -> ". $v. "<br />";
        }
    }
     function get_address_init ($v) { 
                    return get_option($v, '');
    }
     function get_phone_init ($v) { 
                    return get_option($v, '');
    }
    function get_fax_init ($v) { 
                    return get_option($v, '');
    }
    function get_email_init ($v) { 
                    return get_option($v, '');
    }
    function get_hrs_mon_init ($v) { 
                    return get_option($v, '');
    }
    function get_hrs_sat_init ($v) { 
                    return get_option($v, '');
    }
添加快捷码(“自动生成”、“自动生成”);
函数auto_gen(){
$a=数组(
“获取地址”=>“mg\U管理地址”,
“get_phone”=>“mg_admin_phone”,
“获取传真”=>“mg管理传真”,
“获取电子邮件”=>“mg\U管理电子邮件”,
“每天工作时间”=>“每天工作时间”,
“获取小时数”=>“mg工作小时数”
);
foreach($a为$k=>$v){
if(有_短码($v,$k)){
回声“
发现:”.$k; }否则{ 添加快捷码($k,$k.“_init”); $func=$k.“_init”; $func($v); } 回声$k.“->”$v.“
”; } } 函数get_address_init($v){ 返回获取选项($v,”); } 函数get_phone_init($v){ 返回获取选项($v,”); } 函数get_fax_init($v){ 返回获取选项($v,”); } 函数get_email_init($v){ 返回获取选项($v,”); } 函数get_hrs_mon_init($v){ 返回获取选项($v,”); } 函数get_hrs_sat_init($v){ 返回获取选项($v,”); }
函数
add\u shortcode
有一个包含当前短代码的函数,因此同一个回调可以多次使用:

$all = array( 'address', 'phone', 'fax', 'email', 'hrs_mon', 'hrs_sat' );

foreach ( $all as $s )
    add_shortcode( "get_$s", 'general_shortcode' );

function general_shortcode( $atts, $content = '', $shortcode = '' )
{
    switch( $shortcode )
    {
        case 'get_address':
            $return = 'ADDRESS';
        break;
        case 'get_phone':
            $return = 'PHONE';
        break;
        default:
            $return = 'OTHER SHORTCODES';
        break;
    }
    return $return;
}
另一种可能性:

Class AllShortcodes{
    private $all = array(
        "get_address"       =>  "mg_admin_address",
        "get_phone"         =>  "mg_admin_phone",
        "get_fax"           =>  "mg_admin_fax",
        "get_email"         =>  "mg_admin_email",
        "get_hrs_mon"       =>  "mg_work_hrs_mon_frd",
        "get_hrs_sat"       =>  "mg_work_hrs_sat"
    );

    public function __construct() {
        foreach ( $this->all as $key => $value )
            add_shortcode( $key, array( $this, 'general_shortcode' ) );
    }

    public function general_shortcode( $atts, $content = '', $shortcode = '' )
    {
        return $this->all[$shortcode];
    }
}
$myShortcodes = new AllShortcodes;
请不要。