Apr 8, 2013
Tip and Trick Editorial

How to Override Parent Functions in WordPress Chile Theme

WordPressIn WordPress, using a child theme has become kind of recommended and best practice, and hence it’s getting more and more popular as the way to customize the theme without having to worry that the changes been overwritten during update. Child theme not only can customize the look and feel, styling and layout of a website by tweaking the CSS, it also allows additional custom functions to be added to the WordPress, supplementing the functionality provided by the parent theme.

Unlike stylesheet files where CLASS and ID selectors can be overridden with same-name selectors on child theme even when importing the parental style.css, and template files that override their template namesakes from the parent by simply using a file with the same name, functions.php of child theme does not replace nor override its counterpart from the parent theme. In addition, many advanced themes make use of multiple PHP files to include and feed the required functions, such as custom-functions.php to WordPress.

Thus, attempting to override functions from parent theme by placing a functions.php or custom-functions.php in the same directory structure won’t work. In order to override a parent function, a new function must be used to purposely remove the function from its action hook during the WordPress bootstrap ‘init’ or ‘after_setup_theme’ phase. Then, the new function can be added accordingly.

Step 1: Remove parental function

Here’s an example of function which can be placed in child theme’s functions.php to remove a function named some_func() from the parent theme.

function remove_parent_functions() {
remove_action('wp_head','some_func',7);
}
add_action('init','remove_parent_func');

The values for the parameters of remove_action in the function above should be exactly the same (with the same action hook tag and priority) on how the original parent function is called, which is declared via add_action or add_filter in the parent’s theme files, which looks like:

add_action('wp_head',"some_func',7);

[note color=”#FFCC00″]Some functions in the themes are called and hooked earlier than the ‘init’ phase, and thus may not be removed during the ‘init’ phase. In this case, use the ‘after_setup_theme’ phase instead.

For example, add_action('after_setup_theme','remove_parent_func');[/note]

Step 2: Add and active the overridden function

After the original function from the parent is removed, the function needs to be replaced with a new function in the child. To replace the original function, add in the new function into child theme’s functions.php, and then call it at the same action hook phase and same priority with add_action() function.

For example,

function child_new_func() {
....
}
add_action('wp_head','child_new_func',7)

Alternative functions override method for WordPress default themes

Some themes provide an easy way to override functions from the parent in child theme. For example, the themes that packaged and came default with WordPress, Twenty Ten, Twenty Eleven and Twenty Twelve, have built-in feature that allows user to override certain functions by defining theme first in the child theme’s functions.php file. The condition is that these functions must be wrapped in a function_exists() call. The child theme’s functions.php file is included before the parent theme’s file, so the child theme functions would be used.

In such case, in order to override twentytwelve_comment() from the child theme, add your own twentytwelve_comment() function into the child theme’s functions.php.

There may be other methods of overriding method for the themes you used, check out the documentation and manual or check with developer to find it out.

Pin It on Pinterest

Share This

Share This

Share this post with your friends!