2

I'm developing a child theme following the codex

I need to redefine a function in functions.php and that guide says I can do this by adding the code:

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

That's what I did, but I get the following error

Fatal error: Cannot redeclare reg_set_p() (previously declared in /web/htdocs/www.antonellofacchetti.it/home/smg/wp-content/themes/pasw2015-child/functions.php:6) in /web/htdocs/www.antonellofacchetti.it/home/smg/wp-content/themes/pasw2015/functions.php on line 71

Should I assume that something has changed in latest wp version (using 4.0)?

6

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

  • 1
    +1 for clearness and completeness. Just a little note: child theme can (but not have to) use the if: once child is loaded first, function_exists returns false, so the conditional is true and the function is defined. – gmazzap Nov 12 '14 at 21:41
  • @G.M. Thanks for your feedback. Your statement is true, but I always try to emphasize not to add unnecessary code. A child theme can't have a grandchild theme, so I believe there is no need to check if a function exists. It is like if( function_exists( 'register_sidebar' ) ) . It is not wrong, but totally unnecessary. register_sidebar is not a user-defined function and will always exists until the day the core developers decide to remove it, which will be never, maybe depreciated, yes, but not removed. – Pieter Goosen Nov 13 '14 at 4:30
  • thanks a lot for the explanation. Really my mistake was that I put the if statement in the child, while it should be in the parent. I should then assume that a correct way to build the functions.php file in a thene is to always use that if statement when declaring functions, so that it is possible "childing" the theme with no problem. – Antonello Facchetti Nov 13 '14 at 12:09
  • @AntonelloFacchetti You are absolutely correct there. Even the themes I write for myself I include that if statement. It makes it so much easier for child theme authors to modify your parent theme as needed. Don't forget to accept my answer by clicking on the checkmark next to it. This will "mark" this issue as resolved. Thank you. :-) – Pieter Goosen Nov 13 '14 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.