How I fixed the “cannot redeclare _checkactive_widgets()” error in WordPress child themes

So, I had a problem.  For a while I’ve noticed a whole bunch of code being tacked on to my functions.php file.  I’ve had no idea where it came from since I certainly didn’t write it, so I generally just removed it when I saw it.  Then I noticed this weird php error when I built a child theme saying “cannot redeclare _checkactive_widgets()”.  After a few minutes, I realized that it was that same piece of code that’s been tacked onto my functions.php file, only now, since it’s a child theme with two functions.php files, it’s there twice (redeclared).  What gives?

From what I can tell, indirectly (because I’ve found zero documentation on this issue, and I’ve Googled my fingers numb trying), the _checkactive_widgets function basically looks for whatever widgets you have on your current theme and makes sure they get saved when you switch themes so you don’t have to start over.  A great idea.  But if you’re using a child theme, you get my problem.  The only thing I could think of doing is deleting that piece of code, but, from some trial and error, as far as I can tell, whenever you do just about anything in the back-end, it automagically regenerates the code.  So here’s how you fix it:

Using the “Tip for theme developers” note in the Child Themes entry in the WordPress Codex as a guide, I added this to my functions.php file:

if (!function_exists('_checkactive_widgets')) {
	// do nothing
}

This says “if the _checkactive_widgets function doesn’t exist, do nothing.” If it didn’t exist, presumably WordPress would add it in, and this prevents that. It already exists in the parent theme, and the parent’s functions.php gets run next, so this prevents overlap. This has been bugging the hell out of me for weeks, so hopefully it helps someone else.

You might also be interested in these:

  1. How to add customizable header images and customizable backgrounds to your WordPress theme
  2. Using the WordPress embed shortcode for YouTube, Vimeo, more
  3. How to upgrade your Museum Theme to the most current version after you’ve made changes to the files


Posted

in

by