How to Override WordPress Parent Theme Functions – The Right Way

Override WordPress Parent Theme Functions

A child theme is a theme that inherits the functionality and styling of another theme, called the parent theme. Child themes are the recommended way of modifying an existing theme.
Codex

No doubt that using a child theme makes it super easy for you to edit the styles and the functions of the main (parent) theme.

Creating a child theme is a very simple process. But overriding the parent theme functions can be tricky. So, let me explain how to override WordPress parent theme functions without having any problems or fatal errors such as ‘PHP Fatal error: Cannot redeclare function‘.

Override WordPress Parent Theme Functions – The Right Way

When you want to override one of the parent theme template files, all you need is to copy the template file from the parent theme, paste it to your child theme folder and start editing it.

Example: If you want to override your parent theme page.php file, copy it from the parent theme folder and paste it in the child theme folder.

This is not the case with the functions from functions.php file or the other functions that exist in the different theme folders such as /includes/, /framework/, etc. To override a function, it has to be wrapped in an IF statement of function_exists.

Example: To override a function from your parent theme, typically you would copy the function and paste it in your child theme functions.php
But the function from the parent theme must be wrapped in an IF statement like the following:
if ( ! function_exists( 'parent_theme_function' ) ) :

function parent_theme_function() {

	//do_something
}
endif;

Then, you can copy the function to your child theme’s functions.php file as following:

function parent_theme_function() {

	//do_something_else
}

This means that you only can override the functions that are wrapped in a function_exists IF statement.

Hopefully you'll come back soon!
Get More Stuff
WordPress Multilingual, Performance, SEO, Reviews and HOT Deals. One email per week!
Stay Updated
Give it a try, you can unsubscribe anytime.
Close
GET MORE STUFF
IN YOUR INBOX
WordPress Multilingual, Performance, SEO, Reviews and HOT Deals. One email per week!
Stay Updated
Give it a try, you can unsubscribe anytime.
12528e0e3dc5783e6bfbccfeaf5dd0d3%%%%%%%%%%%%%%%%
Share This

Do you find this interesting?

Share it with your friends!