Call to undefined function Carbon_Fields\Provider\add_filter() with Bedrock / PHPUnit
Version
- Carbon Fields: 3.1
- WordPress: 5.4
- PHP: 7.3
Expected Behavior
I just installed the carbon-field-icon package (https://packagist.org/packages/htmlburger/carbon-field-icon) and I have the following error:
Uncaught Error: Call to undefined function Carbon_Fields\Provider\add_filter() in ...\web\app\libs\carbon-fields\core\Provider\Container_Condition_Provider.php on line 254
I tried to create a custom field, but I have the same problem ... I’m using Bedrock for the setup.
My composer.json
"extra": {
"installer-paths": {
"web/app/libs/{$name}/": ["htmlburger/carbon-fields", "htmlburger/carbon-field-icon"],
"web/app/mu-plugins/{$name}/": ["type:wordpress-muplugin"],
"web/app/plugins/{$name}/": [ "type:wordpress-plugin" ],
"web/app/themes/{$name}/": ["type:wordpress-theme"]
}
},
Fix
https://github.com/htmlburger/carbon-fields/issues/899#issuecomment-670926826 Careful, this is a temporary solution. https://discourse.roots.io/t/global-autoload/18863/2
I'm seeing the same issue with carbon-field-number when running phpunit. It seems to stem from field.php file being autoloaded which automatically runs some code as part of extend() that tries to use add_filter() whether or not WordPress is actually loaded at the time. I'm not sure what the proper fix would be but in the meantime I just added a test to check in the register() method of core/Provider/Container_Condition_Provider.php:
if ( defined( 'ABSPATH' ) ) {
$this->install_container_conditions( $ioc );
}
Are there any changes on this issue?
Experiencing the same issue when running phpunit after installing it through composer.
Carbon Fields: 3.1
WordPress: 5.4.2
PHP: 7.3.11
Fatal error: Uncaught Error: Call to undefined function Carbon_Fields\Provider\add_filter() in /Users/dariah/Documents/Dev/sample_wordress/wp-content/plugins/wp-pp-plugin/vendor/htmlburger/carbon-fields/core/Provider/Container_Condition_Provider.php:254
Stack trace:
#0 /Users/dariah/Documents/Dev/sample_wordress/wp-content/plugins/wp-pp-plugin/vendor/htmlburger/carbon-fields/core/Provider/Container_Condition_Provider.php(21): Carbon_Fields\Provider\Container_Condition_Provider->install_container_conditions(Object(Carbon_Fields\Pimple\Container))
#1 /Users/dariah/Documents/Dev/sample_wordress/wp-content/plugins/wp-pp-plugin/vendor/htmlburger/carbon-fields/core/Pimple/Container.php(274): Carbon_Fields\Provider\Container_Condition_Provider->register(Object(Carbon_Fields\Pimple\Container))
#2 /Users/dariah/Documents/Dev/sample_wordress/wp-content/plugins/wp-pp-plugin/vendor/htmlburger/carbon-fields/core/Carbon_Fields.php(385): Carbon_Fields\Pimple\Container->register(Object(Carbon_Fields\Provider\Container_Condition_Provider) in /Users/dariah/Documents/Dev/sample_wordress/wp-content/plugins/wp-pp-plugin/vendor/htmlburger/carbon-fields/core/Provider/Container_Condition_Provider.php on line 254
Hi @piggydoughnut ,
Are you using Bedrock ?
Hey @dimitriBouteille!
Ah, actually not. I missed that in your question. I just did a simple setup using composer https://phpunit.de/
Hello Dimitri,
I had the same error while trying to create a new custom field into a plugin (i.e. into a theme).
I think that Bedrock try to load the custom field before WordPress is loaded, that's the reason why you get this error.
I handled it by removing the autoload of my custom field anywhere, then I generated the autoload with composer dump-autoload. Finally I required the autoload.php file from my plugin initialisation and it works.
Please, note that, this a temporary solution, you have to fix the autoload before sending it to a production environment.
Hi @piggydoughnut and @mihdan
@hafrang found a solution to fix the bug. Careful, as Hafrand says, this is a temporary solution.
Do not hesitate to test this fix and make a return :)
@dimitriBouteille I don't think this should be closed, it isn't resolved yet, it is only a hack
Hello @peterkracik , I’m reopening the issue but I’m not sure he has a new answer :(
Do not hesitate to propose a solution to fix this problem :)
@jorostoyanov Do you have any idea how to fix this problem?