Optimize shortcode callbacks

I created a plugin to add some shortcodes in my WordPress site. But I’m a PHP newbie, so I believe it may have some errors or ways to optimize it.

It’s working fine, and apparently there are no problems. Memory usage is at 00.04MByte. Could you please take a look? Thank you very much!

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

Related posts

Leave a Reply

1 comment

  1. There is a very important programming principle: DRY – Don’t repeat yourself.
    Whenever you realize you are repeating almost the same job try to write an abstraction.

    For your pad* shortcodes this means:

    function get_padding( $atts ) 
    {
        $args = shortcode_atts( 
            array( 'num' => 10 ), 
            $atts 
        );
        return str_repeat( ' ', (int) $args['num'] );
    }
    add_shortcode('pad', 'get_padding');
    

    Now you can use the shortcode [pad] like this:

    [pad num=5]
    [pad num=10]
    [pad num=12000]
    [pad num=18]
    

    The advantage is: if you decide to change the inner logic of your shortcodes you have to touch just one function, not a dozen.

    See also this answer.


    There is another possibility: Use one callback for multiple shortcodes. This uses the third parameter each shortcode callback gets.

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

    Related