从数组自动生成短代码:
您可以尝试以下短代码自动机:
/**
* 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\' );
然后,您可以使用以下工具从主题/插件进行测试:
echo do_shortcode( "[get_address]" );
echo do_shortcode( "[get_phone]" );
echo do_shortcode( "[get_fax]" );
或使用以下方法进行测试:
[get_address]
[get_phone]
[get_fax]
在您的帖子/页面内容中。
下面是我们的演示类定义:
/**
* 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