Optimize shortcode callbacks

时间:2013-01-04 作者:drabello

我创建了一个插件,在我的WordPress站点中添加了一些短代码。但我是一个PHP新手,所以我相信它可能有一些错误或优化它的方法。

它工作正常,显然没有问题。内存使用率为00.04MB。你能看看吗?非常感谢!

add_action( \'wp_enqueue_scripts\', \'prefix_add_my_stylesheet\' );

    function prefix_add_my_stylesheet() {
        // Respects SSL, Style.css is relative to the current file
        wp_register_style( \'prefix-style\', plugins_url(\'style.css\', __FILE__) );
        wp_enqueue_style( \'prefix-style\' );
    }

/* Adsesnse Shortcode */
function get_adsense($atts) {
    return \'THE_ADSENSE_SCRIPT_GOES_HERE
\';
}
add_shortcode(\'adsense\', \'get_adsense\');

/*Padding Shortcodes*/ 
function get_pad5($atts) {
    return \'<div class="pad5"></div>\';
}
add_shortcode(\'pad5\', \'get_pad5\');

function get_pad10($atts) {
    return \'<div class="pad10"></div>\';
}
add_shortcode(\'pad10\', \'get_pad10\');

function get_pad20($atts) {
    return \'<div class="pad20"></div>\';
}
add_shortcode(\'pad20\', \'get_pad20\');

function get_pad25($atts) {
    return \'<div class="pad25"></div>\';
}
add_shortcode(\'pad25\', \'get_pad25\');

function get_pad50($atts) {
    return \'<div class="pad50"></div>\';
}
add_shortcode(\'pad50\', \'get_pad50\');

function get_pad75($atts) {
    return \'<div class="pad75"></div>\';
}
add_shortcode(\'pad75\', \'get_pad75\');

function get_pad100($atts) {
    return \'<div class="pad100"></div>\';
}
add_shortcode(\'pad100\', \'get_pad100\');

function get_clr($atts, $content=null) {
     return \'<div class="clr">\' . $content . \'</div>\';
}
add_shortcode( \'clr\', \'get_clr\' );

/* Alert Boxes */
function get_alertblue($atts, $content=null) {
     return \'<div class="ultra_alert_box ultra_box-blue">\' . $content . \'</div>\';
}
add_shortcode( \'alertblue\', \'get_alertblue\' );

function get_alertgreen($atts, $content=null) {
     return \'<div class="ultra_alert_box ultra_box-green">\' . $content . \'</div>\';
}
add_shortcode( \'alertgreen\', \'get_alertgreen\' );

function get_alertyellow($atts, $content=null) {
     return \'<div class="ultra_alert_box ultra_box-yellow">\' . $content . \'</div>\';
}
add_shortcode( \'alertyellow\', \'get_alertyellow\' );

function get_alertred($atts, $content=null) {
     return \'<div class="ultra_alert_box ultra_box-red">\' . $content . \'</div>\';
}
add_shortcode( \'alertred\', \'get_alertred\' );

function get_alertgray($atts, $content=null) {
     return \'<div class="ultra_alert_box ultra_box-gray">\' . $content . \'</div>\';
}
add_shortcode( \'alertgray\', \'get_alertgray\' );

/* Style Boxes */

function get_stylegreen($atts, $content=null) {
     return \'<div class="ultra_style_box ultra_style-green">\' . $content . \'</div>\';
}
add_shortcode( \'stylegreen\', \'get_stylegreen\' );

function get_styleblue($atts, $content=null) {
     return \'<div class="ultra_style_box ultra_style-blue">\' . $content . \'</div>\';
}
add_shortcode( \'styleblue\', \'get_styleblue\' );

function get_styleyellow($atts, $content=null) {
     return \'<div class="ultra_style_box ultra_style-yellow">\' . $content . \'</div>\';
}
add_shortcode( \'styleyellow\', \'get_styleyellow\' );

function get_stylered($atts, $content=null) {
     return \'<div class="ultra_style_box ultra_style-red">\' . $content . \'</div>\';
}
add_shortcode( \'stylered\', \'get_stylered\' );

function get_stylegray($atts, $content=null) {
     return \'<div class="ultra_style_box ultra_style-gray">\' . $content . \'</div>\';
}
add_shortcode( \'stylegray\', \'get_stylegray\' );

function get_stylewhite($atts, $content=null) {
     return \'<div class="ultra_style_box ultra_style-white">\' . $content . \'</div>\';
}
add_shortcode( \'stylewhite\', \'get_stylewhite\' );

1 个回复
最合适的回答,由SO网友:fuxia 整理而成

有一个非常重要的编程原则:DRY – Don’t repeat yourself.<当你意识到你在重复几乎相同的工作时,试着写一个抽象。

为您的pad* 短代码这意味着:

function get_padding( $atts ) 
{
    $args = shortcode_atts( 
        array( \'num\' => 10 ), 
        $atts 
    );
    return str_repeat( \' \', (int) $args[\'num\'] );
}
add_shortcode(\'pad\', \'get_padding\');
现在您可以使用快捷码[pad] 像这样:

[pad num=5]
[pad num=10]
[pad num=12000]
[pad num=18]
优点是:如果您决定更改短代码的内部逻辑,您只需触摸一个函数,而不是十几个。

另请参见this answer.

还有一种可能性:使用one 的回调multiple 短代码。这使用the third parameter 每个短代码回调都会得到。

function multipass( $atts, $content = \'\', $shortcode = \'\' )
{
    $args = shortcode_atts(
        array (
            \'name\' => \'example\'
        ),
        $atts
    );

    $name    = esc_attr( $args[\'name\'] );
    $content = esc_textarea( $content );

    if ( \'textarea\' === $shortcode )
        return "<textarea name=\'$name\'>$content</textarea>";

    if ( \'input\' === $shortcode )
        return "<input name=\'$name\' value=\'$content\' />";
}
add_shortcode( \'textarea\', \'multipass\' );
add_shortcode( \'input\',    \'multipass\' );
相关的Organizing shortcodes. How to display all of them and their attributes?
  • Show shortcode without executing it
  • Execute shortcode only in another shortcode
  • What characters are allowed as a shortcode tag and how should they be sanitized?
  • 结束