Jump to content

Recommended Posts

First post on this forum here. Mainly a frontend developer for WordPress as well as SEO, but getting into backend development more and more.

 

Been having an issue with a site and trying to solve one particular error. When I send a contact form on a WordPress site I maintain for a client and built a theme for a year ago I now get the error:

 

[04-Feb-2015 07:07:28] PHP Fatal error:  Cannot redeclare class My_meta_box in /../../wp-content/themes/cosily/inc/meta-boxes.php on line 416

That is where a class for meta boxes starts.

foreach ($meta_boxes as $meta_box) {
    $my_box = new My_meta_box($meta_box);
}
 
 
class My_meta_box {
 

    protected $_meta_box;

....

 

But when I grep for that word to see where else that class may be declared in a full backup I see

grep -nr My_meta_box* .

./wp-content/themes/cosily/inc/meta-boxes.php:412:    $my_box = new My_meta_box($meta_box);

./wp-content/themes/cosily/inc/meta-boxes.php:416:class My_meta_box {

 

So there is only one metion of this class. One time declared as a new class and then followed by the class with all the data.  So why would it mention this error? How can I debug this further?

The usual cause for this is that you've included the file defining the class twice. Go through your code to make sure you are not accidentally doing that. Use the include_once/require_once functions rather than include/require to prevent double inclusion.

Thanks for the feedback. Going through all the includes now. Child theme @ wp-content/themes/cosily-child has an include for meta-boxes.php as well loaded using  

require_once 'inc/meta-boxes.php'; which should load it from that theme's inc directory. This contains a different class

class Vihv_meta_box {
 
    protected $_meta_box;
......

 

The parente theme @ wp-content/themes/cosily loads the meta-boxes.php using the get_template_directory function http://codex.wordpress.org/Function_Reference/get_template_directory using 

require(  get_template_directory() . '/inc/meta-boxes.php');  

which should load the file from that theme.

 

Perhaps it is the same protected variable $_meta_box being loaded? Or is the path somehow interpreted differently from what I think..

I made the require in the parent theme themes/cosily/functions.php a require_once:

require_once(  get_template_directory() . '/inc/meta-boxes.php');

and now the error has changed on the send contact form page:

[04-Feb-2015 08:49:33] PHP Fatal error:  Class 'Vihv_meta_box' not found in /../.../../wp-content/themes/cosily-child/functions.php on line 58

So perhaps the two requires did ask for the same file... Still confused here...

Edited by rhand

Using  require(  get_stylesheet_directory() . '/inc/meta-boxes.php'); to load the other meta-boxes.php file fixed this error  :happy-04: . Only it now shows the same form with message the form was sent. Bug squashed though. So it was an issue with a file loaded twice duplicating the class after all. Thanks a lot for the feedback!

I don't know if it's applicable to your situation (or why it's happening, honestly) but recently I've come across a couple situations where using get_template_directory() or get_template_directory_uri() from a child theme will return the parent theme's directory. Which, obviously, breaks a lot of things. I wrote the following method into one of my functions classes to get around it:

private function getTemplateDirectory(){
	$curTheme = \wp_get_theme();
	return str_replace(strtolower($curTheme->get('Template')),strtolower($curTheme->get('Name')),\get_template_directory_uri());
}

Not sure if it helps, just thought I'd throw that out there.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.