Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I was wondering if this was a decent way of handling this function. Please take a look.

// Use Less Mixins from Theme Options to adjust Stylesheets before Parsing Less to CSS
add_filter( 'less_vars', 'wellies_less_vars', 10, 2 );
function wellies_less_vars( $vars, $handle ) {

    //Necessary for UpTheme Framework
    global $up_options;

    //Grab Theme option and check if it's empty and default to color if it is.
    $linkcolor = empty($up_options->linkcolor) ? '#08c' : $up_options->linkcolor;
    $linkcolorhover = empty($up_options->linkcolorhover) ? '#03c' : $up_options->linkcolorhover;
    $networknavstart = empty($up_options->networknavstartcolor) ? '#333' :$up_options->networknavstartcolor;
    $networknavend = empty($up_options->networknavendcolor) ? '#222' :$up_options->networknavendcolor;
    $sitenavstart = empty($up_options->sitenavstartcolor) ? '#333' :$up_options->sitenavstartcolor;
    $sitenavend = empty($up_options->sitenavendcolor) ? '#222' :$up_options->sitenavendcolor;

    //Set Less Mixins to Variables above
    $vars = array(
        'linkColor' => $linkcolor,
        'linkColorHover' => $linkcolorhover,
        'networkNavBarStart' => $networknavstart,
        'networkNavBarEnd' => $networknavend,
        'siteNavBarStart' => $sitenavstart,
        'siteNavBarEnd' => $sitenavend,
    );
    return $vars;
}
//Enqueue Stylesheets if not on dashboard.
function wellies_css_loader() {
    if ( ! is_admin() )
        wp_enqueue_style('bootstrap', get_bloginfo('template_directory').'/library/css/lib/bootstrap.less',  array(), '', 'screen, projection');
    wp_enqueue_style('themes', get_bloginfo('template_directory').'/library/css/theme.less', array(), '', 'screen, projection');
}
add_action('wp', 'wellies_css_loader');

It is currently working but I don't want to have it slow down the site if I can optimize it somehow.

share|improve this question
Try to be more specific in your title and question. E.g Efficient how? I do not see any use of caching. Additionally there is a Wordpress stack exchange as well – Jonnybojangles Jan 29 '12 at 16:27

1 Answer

No, this can be written a lot easier. There is no need to create variables when you are not going to vary the value. It actually only makes things harder to read. Assuming you are using PHP 5.3+ consider this:

// It is odd that you aren't using $vars in your method.
// I'll ignore that for now.
function wellies_less_vars( $vars, $handle ) {

    //Necessary for UpTheme Framework
    // This is not nice, but I'll ignore that too. 
    global $up_options;

    //Set Less Mixins to Variables above
    // Grab Theme option and check if it's set and use that, otherwise use a
    // default value.
    // Now there is beauty in the alignment IMO.
    return array(
        'linkColor'          => $up_options->linkcolor            ?: '#08c',
        'linkColorHover'     => $up_options->linkcolorhover       ?: '#03c',
        'networkNavBarStart' => $up_options->networknavstartcolor ?: '#333',
        'networkNavBarEnd'   => $up_options->networknavendcolor   ?: '#222',
        'siteNavBarStart'    => $up_options->sitenavstartcolor    ?: '#333',
        'siteNavBarEnd'      => $up_options->sitenavendcolor      ?: '#222');
}

There is a small difference in functionality, but a large difference in readability. The ?: uses the value on the left if it evaluates to true (generally a set variable except false), or the value on the right if it is not.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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