Modify the WordPress Theme Functions File

WordPress child themes, or a subset of instructions based on the parent theme, can provide a theme functions file, or a functions.php file, like they can provide template files. Unlike template files, the functions.php file of a child theme does not override the file of the parent theme.


When a parent theme and a child theme each have a functions.php file, both the parent and child functions.php files are run by WordPress, simultaneously. The child theme's functions.php file runs first, and the parent theme's functions.php file runs second. This is intentional because the child theme can replace functions defined in the parent theme's functions.php file.


However, this works only if the functions are set up to allow this. As an example, we will use and modify a child theme called TwentyTen Child, based on the WordPress theme Twenty Ten.


The Twenty Ten functions.php file defines a twentyten_setup function. This function handles the configuration of many theme options and activates some additional features. Child themes can replace this function to change the default configuration and features of the theme, too.


The following lines of code summarize how the functions.php file allows this to happen:


if ( ! function_exists( 'twentyten_setup' ) ):
function twentyten_setup() {
// removed code
}
endif;

Wrapping the function declaration in the if statement protects the site from breaking in the event of a code conflict and allows a child theme to define its own version of the function.


In the TwentyTen Child theme, you can see how modifying this function affects the theme. Add a new twentyten_setup function that adds post thumbnails support to the TwentyTen Child theme's functions.php file:


<?php
function twentyten_setup() {
add_theme_support( 'post-thumbnails' );
}

The result of this change is that the child theme no longer supports other special WordPress features, such as custom editor styling, automatic feed link generation, internationalization and location, and so on.


The takeaway from this example is that a child theme can provide its own custom version of the function only because the parent theme wraps the function declaration in an if block that checks for the function first.




dummies

Source:http://www.dummies.com/how-to/content/modify-the-wordpress-theme-functions-file.html

No comments:

Post a Comment