How to Modify the WordPress Theme Functions File

Like WordPress template files, child themes can also provide a theme functions file, or a functions.php file. 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, WordPress runs both the parent and child functions.php files 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.

The Twenty Thirteen functions.php file defines a twentythirteen_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:

function twentythirteen_setup()

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 TwentyThirteen Child theme, you can see how modifying this function affects the theme. Add a new twentythirteen_setup function that adds post thumbnails support to the TwentyThirteen Child theme’s functions.php file:

<?php
function twentythirteen_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.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.