Changing Theme Functions

This example might not be up to date and cause of this might not work. It is only here to demonstrate the idea.

Whenever you make modification in theme functions, you are facing risk of losing your changes with theme update. As we already know using child theme saves us in this situation.

Lets look at example situation when you asked for some modification on our support forum and you received code that changes few lines in PHP.

Lets say you had to change code:

get_search_form(false)

to

'Search form:'.get_search_form(false)

First we have to find in which function was this change. We look at code:

/*
 * Header search form
 */
if(!function_exists('a13_header_search')){
    function a13_header_search() {
        return
            '<div class="search-container">'.
            '<div class="search">'.
            '<span class="icon-search open tool"></span>'.
            get_search_form(false).
            '<span class="icon-cross close tool"></span>'.
            '</div>'.
            '</div>';
    }
}

Now that we found whole function body, instead of doing modification in main theme code, we first copy this function to child theme.
To do it open functions.php in child theme and drop above code at end of file. There will be already some code in this file, it so you should add your code after such comment code:

/*
 * Add here your functions below, and overwrite native theme functions
 */

So in result you should have(in functions.php file):

/*...some default code above... */
 
 
/*
 * Add here your functions below, and overwrite native theme functions
 */
 
/*
 * Header search form
 */
if(!function_exists('a13_header_search')){
    function a13_header_search() {
        return
            '<div class="search-container">'.
            '<div class="search">'.
            '<span class="icon-search open tool"></span>'.
            get_search_form(false).
            '<span class="icon-cross close tool"></span>'.
            '</div>'.
            '</div>';
    }
}

Now you can make your modification and in final result have:

/*...some default code above... */
 
 
/*
 * Add here your functions below, and overwrite native theme functions
 */
 
/*
 * Header search form
 */
if(!function_exists('a13_header_search')){
    function a13_header_search() {
        return
            '<div class="search-container">'.
            '<div class="search">'.
            '<span class="icon-search open tool"></span>'.
            'Search form:'.get_search_form(false).
            '<span class="icon-cross close tool"></span>'.
            '</div>'.
            '</div>';
    }
}

You have now overwritten main theme function, and your version will be loaded.

If you activate main theme instead, then your change won’t be loaded.

Most interesting functions you will find in advance/utilities(in main theme) , but there are more functions that you can change in child theme. All functions that can be overwritten has before its definition code: if(!function_exists('function_name')).

If you wish to change other functions ask on our forum for support in that case, or just don’t bother with child theme and change main theme(if you understand implications;-) ).

Was this article helpful to you? Yes No