A quick note to theme devs: use if( ! function_exists() )

Parent & Child Themes in WordPress

I’ve been working on modifying an existing theme for a client of mine this week. I created a child theme (because that’s the right thing to do), and I wanted to override some functions that had been declared in the parent theme, to get the theme to behave the way the client needed it to.

The trouble is, since the functions.php file of both the parent theme and the child theme are loaded when using a child theme, you cannot redeclare existing functions, because it will throw up a PHP error.

The correct way to add functions to your functions.php file when you’re creating a theme is to wrap each function in a if( !function_exists() ) conditional first. Because the child theme’s functions.php file is loaded first, it will declare the function, and when the parent theme’s functions.php file is loaded, the if( !function_exists() ) wrapper will make it skip over the function, because you’ve already declared it in your child theme.

So, when you’re developing themes, please make sure to declare your functions appropriately.

One thought on “A quick note to theme devs: use if( ! function_exists() )”

  1. tyler says:

    Good tip Dave! Something I need to start doing more of myself. :)

Leave a Reply