• Resolved pro100tom

    (@pro100tom)


    Hi, friends. I have recently started to learn WordPress. In this thread there is a heading called “Using functions.php”. Below that heading there is a “TIP FOR THEME DEVELOPERS.” heading. It is said there that the child theme function can replace the parent one by adding a conditional statement to it:

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

    My question is: if we have the same function in both child and parent php files and if we add a conditional statement into the child function to check whether it already exists (It is said that child functions run before parent ones then it definitely does not exist) then how this condition helps to skip the execution in the parent’s function? After the function was executed in the child theme, it executes in the parent one. But there is no conditional statement there, so it must be executes no matter what happens. How all this logic works?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’m not sure why you would be using ! function_exists inside a child theme unless you have a very unusual circumstance. Basically you have the right understanding. This statement is used in parent themes so that it is easy for child themes to create their own definitions for these themes. Essentially you could copy the function from the parent them, bring it into your child theme, and then edit it. If they didn’t have this in the parent theme, then if you called it in the child theme it would break the site as it would be a fatal error. Does that make sense?

    Thread Starter pro100tom

    (@pro100tom)

    It makes perfect sense, thank you! Probably I misunderstood that topic a bit or it is just written in a bit unclear way. Anyways I am very thankful for your respond and have no questions regarding this topic anymore.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Child and parent function execution issue’ is closed to new replies.