Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple CMB2s and helper-functions.php fatal errors #520

Closed
DevinWalker opened this issue Nov 9, 2015 · 14 comments
Closed

Multiple CMB2s and helper-functions.php fatal errors #520

DevinWalker opened this issue Nov 9, 2015 · 14 comments

Comments

@DevinWalker
Copy link
Contributor

If I have two plugins installed that both use CMB2 for their options/fields I see: Fatal error: Cannot redeclare cmb2_dir() (previously declared in /srv/www/wordpress-default/wp-content/plugins/.../includes/libraries/cmb2/includes/helper-functions.php:18) in /srv/www/wordpress-default/wp-content/plugins/.../includes/libraries/metabox/includes/helper-functions.php on line 18

I see that it's simply required here: https://github.com/WebDevStudios/CMB2/blob/master/init.php#L144

Shouldn't these functions be wrapped in a function_exists() conditional? How can multiple plugins/themes use CMB2 successfully with the helper functions included like this?

@DevinWalker
Copy link
Contributor Author

So you can get around this by slighly modifying the bootstrap include like so:

if ( file_exists( WP_PLUGIN_DIR . '/cmb2/init.php' ) && !defined( 'CMB2_LOADED') ) {
    require_once WP_PLUGIN_DIR . '/cmb2/init.php';
} elseif ( file_exists( GMB_PLUGIN_PATH . '/includes/libraries/metabox/init.php' )&& !defined( 'CMB2_LOADED') ) {
    require_once GMB_PLUGIN_PATH . '/includes/libraries/metabox/init.php';
} elseif ( file_exists( GMB_PLUGIN_PATH . '/includes/libraries/CMB2/init.php' ) && !defined( 'CMB2_LOADED') ) {
    require_once GMB_PLUGIN_PATH . '/includes/libraries/CMB2/init.php';
}

The issue with this is not every plugin and theme author will include CMB2 like this... so, we run into the conflict with these helper functions. I still propose that these are wrapped in function_exist conditionals. Would like to hear your thoughts...

@JPry
Copy link
Contributor

JPry commented Nov 9, 2015

Hey Devin,

Are your plugins requiring the CMB2 bootstrap.php file, or the init.php file?

@DevinWalker
Copy link
Contributor Author

Hey @JPry they are using init.php like the include in my comment above

@jtsternberg
Copy link
Member

There are so many checks in place by the time that require is called, that there should never be a conflict. Including init.php is the correct way to do it.

@DevinWalker
Copy link
Contributor Author

@jtsternberg there are indeed a couple checks in place, but as we see there are still conflicts reported. I suggest just keeping an eye out in the future for this in case it's reported more frequently. Closing for now.

@jtsternberg
Copy link
Member

@DevinWalker first time I've seen this being reported for CMB2, as it's been explicitly built to handle those scenarios. Would love more details for how I can reproduce the issue. We use CMB2 all day every day at WDS, with it being bundled in a variety of our plugins, etc, and have yet to run into this.

@DevinWalker
Copy link
Contributor Author

@jtsternberg the best way to reproduce it is to download and install Give https://wordpress.org/plugins/give and Maps Builder https://wordpress.org/plugins/google-maps-builder/ from the repo. Give includes CMB like:

if ( file_exists( WP_PLUGIN_DIR . '/cmb2/init.php' ) ) {
    require_once WP_PLUGIN_DIR . '/cmb2/init.php';
} elseif ( file_exists( GIVE_PLUGIN_DIR . '/includes/libraries/cmb2/init.php' ) ) {
    require_once GIVE_PLUGIN_DIR . '/includes/libraries/cmb2/init.php';
} elseif ( file_exists( GIVE_PLUGIN_DIR . '/includes/libraries/CMB2/init.php' ) ) {
    require_once GIVE_PLUGIN_DIR . '/includes/libraries/CMB2/init.php';
}

Note: I have added an additional check for && ! defined( 'CMB2_LOADED' ) into the conditionals above as seen here: https://github.com/WordImpress/Give/blob/master/includes/admin/register-settings.php#L1116

Maps Builder was a bit different. I had just included it like so:

require_once GMB_PLUGIN_PATH . 'includes/libraries/metabox/init.php';

https://github.com/WordImpress/Google-Maps-Builder/blob/d90a1f2a786be4257fdb1d0baa1717167bfc6aae/google-maps-builder.php#L219

... but now I've changed it to be like the new Give implementation if you look at the master branch. It appears to have resolved the issue but if you want to reproduce it install both repo version since this hasn't been pushed yet.

The main issue comes from people simply copying the include method from Getting Started: https://github.com/WebDevStudios/CMB2/wiki/Basic-Usage which doesn't actually check if CMB2 has been loaded already.

Let me know if you have any questions ;)

@benoitchantre
Copy link

@jtsternberg same issue for me. I have a custom theme that includes CMB2 with the init file.

if ( file_exists(  __DIR__ . '/inc/cmb2/init.php' ) ) {
    require_once  __DIR__ . '/inc/cmb2/init.php';
}

The CMB2 plugin was also installed to ease the updates of the library before an update of the theme is available.

After the update of the CMB2 plugin to the latest version, I have the following error:

Fatal error: Cannot redeclare cmb2_dir() (previously declared in /home/clients/b9bea0796c13d5f0e5012c76b5730384/web/_wp/wp-content/plugins/cmb2/includes/helper-functions.php:18) in /home/clients/b9bea0796c13d5f0e5012c76b5730384/web/_wp/wp-content/themes/tropiquarium/inc/cmb2/includes/helper-functions.php on line 20

When I change the test in the theme to load CMB2 as suggested by @DevinWalker, there's no more fatal error.

if ( file_exists(  __DIR__ . '/inc/cmb2/init.php' )  && !defined( 'CMB2_LOADED') ) {
    require_once  __DIR__ . '/inc/cmb2/init.php';
}

@jtsternberg
Copy link
Member

Looking into this.

jtsternberg added a commit that referenced this issue Feb 29, 2016
…at (to keep old versions from loading as well). Fixes #520
jtsternberg added a commit that referenced this issue Feb 29, 2016
…at (to keep old versions from loading as well). Fixes #520
@jtsternberg
Copy link
Member

This should be fixed with 2.2.1, please update wherever possible.

@jtsternberg
Copy link
Member

@DevinWalker It definitely was only an issue with 2.2.0, and I wish I would have caught it before the release, but thank you for calling it out earlier.

@DevinWalker
Copy link
Contributor Author

No problem, glad it's addressed

@watson-77
Copy link

CMB2 - v2.2.6, v2.4.2 - multiple plugin with cmb2 not working

@tw2113
Copy link
Contributor

tw2113 commented Aug 24, 2018

@watson-77

What errors are you seeing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants