Parent & Child Themes in WordPress

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

| 1 Comment

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.

Categories: Code & Snippets | Permalink

What next?

Hire me

If you couldn't quite manage this yourself, find it too intimidating, or just don't have the time to do it, you can always hire Dave to do it. Please get in touch so that we can discuss your needs.

Leave a comment

If you have a question, update, or comment about the tutorial, please leave a comment. I try and respond to every comment, though it may take a few days, so please check back soon.

Let a WordPress Expert help you

Do you want to try this, but feel like you need a helping hand, in case something goes wrong? My service, The WP Butler, gives you access to WordPress expertise whenever you need it. Better yet, I'll keep your site backed up, updated and secure, so that you don't have to worry about it. It's all part of the service. Use coupon DIWW and save 15% on all plans.

Visit The WP Butler

Author:

Dave has been tinkering with WordPress for many years, and he now shares his WordPress knowledge here on Do It WIth WordPress to help others realise its impressive power. He can also be hired to help with your WordPress needs. Dave, who is British, is married to his best friend, Marti, with whom he has a beautiful daughter, Ellie. When he's not dabbling with WordPress, he's probably eating Triscuits or hummus, watching an indie film or British TV show, spending time with friends or family, or exploring the world.

One Comment

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

Leave a Reply