Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Depending on how well a parent theme is built, you can use a series of different techniques to either override or overrule functions in that parent theme. By overriding or overruling the functions, you're telling WordPress to ignore the function that's in the parent theme or augment the function that's in the parent theme to produce the result you want. How to override a function depends on how well the theme is written, and also how that function is included in the theme. Most well-written themes wrap functions in conditional statements so that they can easily be overwritten by child themes.
When they are not wrapped in these statements you're usually given either a filter or an action hook that you can hook into or change the actions you can interact with it. Let's a take a look at some of these things in the twentytwelve theme, so you can see how you can interact with it. If you go down to line 28, you'll see an example of how a function is included so that you can override it with your child theme. The function here sets a default content width for the site, meaning if you add an image and it's larger than the value set here--625 pixels--it'll automatically be scrunched down to fit within 625 pixels.
What happens here is WordPress says, if this value is not already set, then use this value. That's because of how functions.php interacts with the functions.php file in your child theme. Unlike with a style sheet where the parent theme style sheet is called first and then the child theme style sheet is called, when it comes to functions.php, the child theme functions.php file is called first and then the parent theme functions.php is called afterwards. Meaning, if you have set content_width in your child theme, this condition says it's already set; therefore, we're not going to set it in the parent theme.
On line 228 you see a different type of conditional statement. Here it says if function exists, or rather if not function exists, meaning if the child theme does not have the function twentytwelve_content_nav, then use this function instead. That also means if you want to create a different content nav function in your child theme, you simply create it inside your child theme and then it will automatically override the function in the parent theme. On line 177 you see a different type of interactive feature.
This is a filter. It says add_filter, wp_title. A filter is different from a function, in that when you have a filter you can pass information to that filter and change the behavior of the function. So this filter interacts with the function above it, this one, twentytwelve_wp_title. It's up here, this whole function. And what you can do in your child theme is add information with a filter that then gets passed into this function and you can change to function through that filter. So it's a more advanced option when you want to change maybe a word or a link or something like that in that function without having to rebuild the function in the process.
Overriding or removing functions allows you to take control of the functionality of your child theme and do things not originally intended by the parent theme author. It can be very useful, but be aware that when you start messing with functions weird things tend to happen, so move slowly. Fortunately, by doing all of this in a child theme you're never going to do anything that will actually break the site, and everything is always reversible.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.