4

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.

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"

2

Auto-generate shortcodes from an array:

You can try the following Shortcode Automat:

/**
 * Setup the Shortcode Automat
 *
 */

function shortcode_automat_setup()
{   
    $settings = 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"
    );

    $sc = new ShortCodeAutomat( $settings );
    $sc->generate();
}

add_action( 'wp_loaded', 'shortcode_automat_setup' );

You can then test it from your theme/plugin with:

echo do_shortcode( "[get_address]" );
echo do_shortcode( "[get_phone]" );
echo do_shortcode( "[get_fax]" );

or test it with:

[get_address]
[get_phone]
[get_fax]

within your post/page content.

Here is our demo class definition:

/**
 * class ShortCodeAutomat
 */

class ShortCodeAutomat
{
    protected $settings = array();

    public function  __construct( $settings = array() )
    {
        $this->settings = $settings;
    }

    public function __call( $name, $arguments )
    {
        if( in_array( $name, array_keys( $this->settings ) ) )
        {
            return get_option( sanitize_key( $this->settings[$name] ), '' );
        }
    }

    public function generate()
    {
        foreach( $this->settings as $shortcode => $option )
        {
            add_shortcode( $shortcode, array( $this, $shortcode ) );
        }
    }
} // end class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.