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

Update 3/31/2014

So it's now almost 4 years after I wrote this post and there's still no new information regarding this issue. I assumed originally that this was some WordPress core function, but WordPress core coding philosophy would prohibit adding code to an individual theme's files. Now, I haven't gone through the code that gets added line-by-line to analyze exactly what it's doing and I'm not going to. But there have been those who have said that this is the result of a worm. Considering I often download themes (and plugins) from clients, I can understand how my local environment could be affected by a worm, but I'm still skeptical that it's that malicious.

Still, here's the REAL fix, since when I search for this issue, my post still shows up on the first page of Google. The below instructions still work (for the current theme) but this will take care of it for good (at least so it seems).

Ready? Here it goes:

Step 1:
Go into every single functions.php file on your server. Delete all the code at the end of the file starting with function _checkactive_widgets().

Step 2:
Repeat until you have done this to all your themes.

Note: Some themes (like Responsive, among others) have functions.php files inside a subdirectory. You need to get those, too.

Once all the files have been cleaned, you should be good to go.

Original post below for posterity (and for the cheap, easy fix).

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.

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

  1. _checkactive_widgets its my big problem when i work on my framework i will try this , but how i can disallow wordpress from edit my functions.php ..??

    • WordPress will add the check for widgets to your functions.php (if it exists) no matter what unless you explicitly tell it not to. If you really wanted to, you could change the permissions of your functions.php and make it read-only, although I don't know off the top of my head how that might affect the functionality of your theme. If there is no functions.php in your child theme, you don't need to worry, but if you're going to add your own custom functions (and, let's face it, that's probably 99% of the reason we want to build child themes, right?), you'll need to add some method for handling WordPress automatically trying to add in the active widgets check. Based on what information they have in the Codex on Child Themes, the most correct way of dealing with this issue is just to say "if this function exists already, don't do anything" and that will prevent WordPress from duplicating your code.

  2. really helpful. WordPress should include the check widget function if the theme is child theme.