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
Add function_exists( 'add_action' ) check to initialization file #1270
Comments
Add function_exists('add_action') check to initialization file. When loading CMB2 via Composer (and autoloading init.php), the init.php will be loaded every time. Usually this is not a problem, however if we are also using Composer to load a PHP development tool (e.g. PHP Codesniffer (phpcs)), when the development tool runs it is NOT in the context of WordPress. Running CMB2's init.php outside of the context of WordPress results in a fatal error because the function add_action() does not exist. By adding this function_exists() check, we can avoid this fatal error when autoloading init.php outside of the context of WordPress. See CMB2#1270
I had the same issue with the latest version of CMB2 , Also, after a simple google search, it seems this issue has crashed few websites ! |
Based on #1271 it's a change that will get included in a future release, we just haven't gotten to the point of actually releasing yet. :D |
@MichaelHabib I just wanted to chime in with some clarification about the issue I originally submitted, I assume your Google search was something like "Uncaught Error: Call to undefined function add_action() in vendor/cmb2/cmb2/init.php". The results that come back for me have pretty "deep" URLs (e.g. http://example.com/wp-content/plugins/cmb2/init.php) and with the sites I checked, if I went to the root of the website instead (e.g. http://example.com/) I didn't see the error. I agree the preferred behavior would be to show nothing, rather than the error message. Based on all of this I'm glad to see this being added in the next release but I understand it is not an high priority to get the next release out. 😀 |
Thanks for the quick updates |
When using CMB2 via composer and using a PHP based tool (e.g. PHP Code Sniffer), the PHP based tool throws an error.
Expected Behavior:
When installing CMB2 and PHP Code Sniffer via composer with a
composer.json
ofI expect the following to run PHP Code Sniffer (
phpcs
)Actual Behavior:
I get a fatal error because the function
add_action()
is undefined.Possible Solution
If we add a
function_exists()
check foradd_action()
before calling it, we can prevent throwing this fatal error.Possible Solution's Risk Level
This seems to be a relatively low risk change. Since we're checking if the function
add_action()
exists directly before calling it, the only behavior change we should get is avoiding throwing this fatal error.Steps to reproduce (I have confirmed I can reproduce this issue on the
develop
branch):composer install
./vendor/bin/phpcs
Your Environment
Mac OS X Command Line
The text was updated successfully, but these errors were encountered: