Is it better to create a function or a variable for current_theme_supports?

Well I just asking myself this, there are two ways I could implement this. What would be a better fit to coding standards?
Or is this just a a matter of personal like?

And what about php and memory, assuming this would be way more complicated and executed very often. Would it cost more memory to create a function or a variable?

Read More

Solution 1:

function stuff(){

    [...]

    $fluid = ( current_theme_supports('greatness') ) ? '-greatness' : '';

    $output = '<div class="box' . $fluid . '">';

    [...]

}

Solution 2:

function stuff_alternative(){

    [...]        

    $output = '<div class="box' . bootgen_is_great() . '">';

    [...]

}

function bootgen_is_great(){

    if ( current_theme_supports( 'greatness' ) )
        return '-greatness';

}

Related posts

Leave a Reply

1 comment

  1. Memory is not the issue here. There are no complex operations, and the difference will be almost unmeasurable.

    What matters is readability: If your stuff function is rather short (less than eight functional steps) and you use the check just once – keep the check in it.
    But if you need the support check in different functions separate it.

    I’d rather optimize the names: greatness is not very descriptive, and while I like fluid layouts – they are not a synonym for great layouts. 🙂
    Also, it is a custom name, so prefix it to avoid collisions with plugins and to tell the uninformed reader (you, one year later) that it is something custom.

    Suggestion for a theme named wpse51275:

    function wpse51275_box()
    {
        // some awesome code …
    
        $box_class = 'box';
        current_theme_supports( 'wpse51275_fluid_layout' ) and $box_class .= '-fluid';
    
        $output = "<div class='$box_class'>";
    
        // more awesome code …
    }