Autogenerate wordpress shortcodes using array?

I was create a shortcode which automatically generate shortcodes with given array key and value. Function names does not generate dynamically.

Note: Array KEY = ShortcodeName and Value = WordPress Option field.

Read More
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 />";
    }
}

There is any possible way to do this.

NOTE:

Here, get_address array key is a shortcode. And it is dynamically generate when It pass though loop. get_address is changable. If I change get_address with get_user_address then get_user_address generate generated. “get_address”, “get_phone” are CHANGABLE at END LEVEL.

Developer also generate shortcodes to access created wp_options useing get_options, simply pushing elements in array. e.g. “shortcode_name” => “option_name”

Related posts

Leave a Reply

2 comments

  1. The function add_shortcode has a third parameter that contains the current shortcode, so the same callback can be used multiple times:

    $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;
    }
    

    Another possibility:

    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;
    
  2. Try following code:

    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, '');
        }