Redefining function in child themes

I think you misunderstood the codex here. You cannot re-declare the same function, except when, and only when, the parent theme has wrapped a function in that conditional statement

if ( ! function_exists( 'theme_special_nav' ) ) {
    function theme_special_nav() {
        //  Do something.
    }
}

What this means is, you can copy the theme_special_nav() without the if condition to your child theme, keep the same name and modify the function as needed.

Example:

function theme_special_nav() {
   //  Modified content.
}

The child theme’s functions.php is loaded first, then the parent theme’s functions.php. So, when the parent theme loads, it will first check if a function with the same name already exists in the child theme, and if it does, then the function in the parent theme is ignored/skipped

If your parent theme don’t have this conditional statement wrapping a function, you have three other choices:

  • Making use of the filters provided within the function if there are any provided by the theme author.

  • If no custom filters are provided, you’ll need to copy the specific function to your child theme, rename it, modify the function as needed, and then look for all instances where the function is called in template files, copy that templates to your child theme and replacing the original calls to the old function with a call to your new function

  • If the function is hooked to an action hook or filter, you can remove it from that specific hook and replace it with your modified function with another name

Leave a Comment