Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Problem with WPAlchemy Metabox... Can't use in 2 plugins WordPress

  • SOLVED

I built 2 plugins and I've used WPAlchemy Metabox class to build both. I love it, and it works extremely easy. But I have quite a major problem.

Apparently you can't have 2 plugins or instances of the WPAlchemy Metabox class. I've tried wrapping the class if if exists and can get things to work, but then nothing saves.

Anyone have any ideas on this? or experience with this?

I've added the class to my main plugin like this...

if (!class_exists( 'WPAlchemy_MetaBox' ) ) {
include_once 'metaboxes/MetaBox.php';
}


I've also tried wrapping he actual class as in above as well.

Answers (6)

2011-11-27

Denzel Chia answers:

Hi Armand,

<blockquote>
I can add multiple metaboxes. That's not the problem.

The problem is when I have two seperate plugins both using WPAlchemy class.

I get a cannot redeclare WPAlchemy error.
</blockquote>

You simply need to change the name of the class.
In plugin A, you use original Class WPAlchemy_Metabox in MetaBox.php, no modification to the codes.
In plugin B, you change script name to Class WPAlchemy_Metabox2 in MetaBox.php, no need to change function names within the class.

In theory it is the same as


function this_is_a(){
echo "hello world";
}

function this_is_b(){
echo "hello world";
}


Both does the same thing, but they are not the same function.

This applied for class too.


class WPALchemy_MetaBox
{

//this are the codes within the class
//no need to change

}


class WPALchemy_MetaBox2
{

//this are the codes within the class
//no need to change

}


Both classes does the same thing but they are not the "same" class, after you renamed the class. No changes to how you use it.



Hope I explained it clearly.

Thanks!
Denzel


Armand Morin comments:

That worked. Thanks.

2011-11-26

Utkarsh Kukreti answers:

What exactly did you do when wrapping the class in a class_exists? Could you post the relevant code?


Armand Morin comments:

Here's what I did inside of MetaBox.php

if ( !class_exists( 'WPAlchemy_MetaBox' ) ) {
class WPAlchemy_MetaBox
- rest of class here.
}}

2011-11-26

Romel Apuya answers:

might be the <strong>!</strong>?
remove it.

if ( class_exists( 'WPAlchemy_MetaBox' ) ) {

class WPAlchemy_MetaBox

- rest of class here.

}}


Armand Morin comments:

No that didn't work.


Romel Apuya comments:

are you adding them like this?

include_once 'WPAlchemy/MetaBox.php';

//first metabox...
$custom_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_custom_meta', // underscore prefix hides fields from the custom fields area
'title' => 'My Custom Meta',
'template' => TEMPLATEPATH . '/custom/meta.php'
));


// add a second custom meta box
new WPAlchemy_MetaBox(array
(
'id' => '_custom_meta2',
'title' => 'My Custom Meta #2',
'types' => array('page','events'), // added only for pages and to custom post type "events"
'context' => 'normal', // same as above, defaults to "normal"
'priority' => 'high', // same as above, defaults to "high"
'template' => TEMPLATEPATH . '/custom/meta2.php'
));


Romel Apuya comments:

i think you need to create another instance of the class in metabox.php..


Romel Apuya comments:

try it like this..

include_once 'WPAlchemy/MetaBox.php';

//first metabox...
$custom_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_custom_meta', // underscore prefix hides fields from the custom fields area
'title' => 'My Custom Meta',
'template' => TEMPLATEPATH . '/custom/meta.php'
));


// add a second custom meta box
new WPAlchemy_MetaBox(array
(
'id' => '_custom_meta2',
'title' => 'My Custom Meta #2',
'types' => array('page','events'), // added only for pages and to custom post type "events"
'context' => 'normal', // same as above, defaults to "normal"
'priority' => 'high', // same as above, defaults to "high"
'template' => TEMPLATEPATH . '/custom/meta2.php'
));


Armand Morin comments:

I can add multiple metaboxes. That's not the problem.

The problem is when I have two seperate plugins both using WPAlchemy class.

I get a cannot redeclare WPAlchemy error.

2011-11-26

Kannan C answers:

!class_exists can prevent function re-declaration issue. But you are saying that you cannot save the meta. I didn't used WPAlchemy, let me have a try on that.

i will suggest you to move the class files to a common location (for example: to the currently active theme's folder) when activating the plugin. So you can have a single copy of the class. This is not a good idea but will work. Let me check WPAlchemy class for any other idea.

2011-11-26

Jurre Hanema answers:

Doing what you're doing (using class_exists to prevent class re-declaration) should work. Therefore I suspect that you have some other mistake somewhere.

I created a two testcase plugins that both include and use WPAlchemy_MetaBox successfully. Try my example and see if it works for you (when both plugins are enabled, two metaboxes are added to pages).

If it doesn't work, then the problem is definitely somewhere else. If my plugins work but yours don't, then try studying my example to find out what goes wrong in your code.


Armand Morin comments:

Jurre,

Your plugins did work. In looking at my issue further. I was using an editor area. Oddly enough, text fields, textareas but NOT editor areas.

My mistake for not checking all fields. For some reason the editor areas are not saving everything else seems to save fine. Thanks for narrowing it down.

So my problem lies in the text areas.


Armand Morin comments:

here's the other issue.. BOTH plugins have to have the include wrapped with class_exists or the re-declaration issue comes up.

if someone else is using WPAlchemy then it's not possible for me to change that plugin's statement. See the issue?


Jurre Hanema comments:

Yeah, it's true, if other plugins use WPAlchemy then that's a potential issue. But unfortunately there is no way (that I know of) to fix that.

I will see if I can find out why textareas cause a problem.


Jurre Hanema comments:

What do you mean by "editor area"? I have tested my metabox plugin with regular <textarea>'s and everything works just fine.

The template should look somewhat like this:

<?php $mb->the_field('meta_plugin_1_value'); ?>
<label>Meta value 1:</label>
<textarea name="<?php $mb->the_name(); ?>"><?php echo esc_textarea($mb->get_the_value()); ?></textarea>

2011-11-26

Just Me answers:

try setting a global variable to keep track of whether the class is present or not.