patterncssMinor
Repetitive wordpress PHP styling
Viewed 0 times
phprepetitivewordpressstyling
Problem
I was wondering if this was a decent way of handling this function. Please take a look.
It is currently working but I don't want to have it slow down the site if I can optimize it somehow.
// 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.
Solution
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:
There is a small difference in functionality, but a large difference in readability. The
// 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.Code Snippets
// 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');
}Context
StackExchange Code Review Q#7974, answer score: 2
Revisions (0)
No revisions yet.