if ( !function_exists('solid_home_page_menu_args') ) {
function solid_home_page_menu_args( ) {
$args['show_home'] = true;
return $args;
}
add_filter( 'wp_page_menu_args', 'solid_home_page_menu_args' );
}
I'm trying to figure out what the developer of this wordpress theme was thinking. Please assist. I'll be thankful.
So far I have understood most of the code in the functions.php file apart from the above code.
if ( !function_exists('function_name') )
is used as a wrapper for functions used in a parent theme where function_name
is the name of the function being wrapped by the conditional statement.
Whenever a parent theme author wraps a function in his theme in this conditional statement, it means the following for a child theme author
The child theme author can simply copy the function to the child theme and add/remove functionality within the function as he likes.
The child theme author don't need to change the function name, the same function name can be used. This is the only time where you can have two functions with the same name
The child theme author don't have to change any other template files to change calls to a new function
The child theme's functions.php is loaded before the parent theme's functions.php. This means, if you have copied the function function_name
to your child theme and modified it, it will be declared first before the function function_name
in the parent theme.
Now, right before the function is declared in the parent theme, the conditional statement will first check if a function by the same name function_name
already exists. If it does, then the function is skipped/ignored in the parent theme. If a function with that name is not found, then the function of the parent theme is declared and used
If you are a parent theme author, it is really good practice to wrap your functions in this if ( !function_exists('function_name') )
conditional statement. This will make live easier for child theme authors to modify or remove functions from your parent theme
EDIT
What is the meaning of this?
$args['show_home'] = true;
return $args;
You will need to look at the source code for the wp_page_menu_args
filter inside the function wp_page_menu
.
1146 if ( ! empty($args['show_home']) ) {
1147 if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
1148 $text = __('Home');
1149 else
1150 $text = $args['show_home'];
1151 $class = '';
1152 if ( is_front_page() && !is_paged() )
1153 $class = 'class="current_page_item"';
1154 $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
1155 // If the front page is a page, add it to the exclude list
1156 if (get_option('show_on_front') == 'page') {
1157 if ( !empty( $list_args['exclude'] ) ) {
1158 $list_args['exclude'] .= ',';
1159 } else {
1160 $list_args['exclude'] = '';
1161 }
1162 $list_args['exclude'] .= get_option('page_on_front');
1163 }
1164 }
This hook allows you to set a key/value pair for the variable $args
with a specific key show_home
and the value a boolean of true
which will allow you to display the Home
link in the navigation bar
The Home
link does not show by default in the navigation bar as this key/value pair does not exist. As soon as you set this key/value pair via the filter, then the conditional statement returns true and displays the Home
link
If the solid_home_page_menu_args
function doesn't exist, it declares it and add it as a filter.