Carbon-fields: Fatal error: Uncaught Error: Class 'Carbon_Fields\Carbon_Fields' not found on theme/frontend

Created on 14 Jul 2017  路  4Comments  路  Source: htmlburger/carbon-fields

Hi, I've installed CF 2.0.1 via composer in my theme directory, and followed the steps to setup fields in the admin: all works great there, but when I access the theme itself (ie. trying to view the home page in the browser) I get this error:

Fatal error: Uncaught Error: Class 'Carbon_Fields\Carbon_Fields' not found in /var/www/wp-content/themes/THE_THEME/functions.php on line聽22

Fatal error: Uncaught Error: Class 'Carbon_Fields\Carbon_Fields' not found in /var/www/wp-content/themes/THE_THEME/functions.php on line聽22

functions.php line 22 actually has Carbon_Fields\Carbon_Fields::boot(); inside the crb_load function:

function crb_load() {
    require_once( 'vendor/autoload.php' );
    Carbon_Fields\Carbon_Fields::boot();
}

add_action( 'after_setup_theme', 'crb_load' );

also, something to note, when installing it from _within_ the theme directory in creates a wp-content/plugins/carbon-fields within the theme's directory itself, so it looks like THE_THEME/wp-content/plugins/carbon-fields..

thanks!

[type] question

Most helpful comment

Maybe try?

require_once( __DIR__ . '/vendor/autoload.php' );

add_action( 'plugins_loaded', array( 'Carbon_Fields\\Carbon_Fields', 'boot' ) );

Regarding the folder path, you can change it with mnsami/composer-custom-directory-installer. Example:
https://github.com/htmlburger/carbon-fields/issues/273

EDIT: Here is a longer example of what I'm doing...

public function __construct() {
  // Initialize Carbon Fields and verify dependencies
  add_action( 'plugins_loaded', array( 'Carbon_Fields\\Carbon_Fields', 'boot' ) );
  add_action( 'carbon_fields_loaded', array($this, 'load_plugin') );
}

public function load_plugin() {
  if(!$this->verify_dependencies()) return;

  // Enqueue scripts
  new EnqueueScripts();

  // Add admin settings page(s)
  new Settings();

  // Run core plugin logic
  new Core();
}

private function verify_dependencies() {
  // Check if outdated version of Carbon Fields loaded
  $error = false;

  if(!defined('\\Carbon_Fields\\VERSION')) {
    $error = '<strong>' . self::$settings['data']['Name'] . ':</strong> ' . __('A fatal error occurred while trying to load dependencies.');
  } else if( version_compare( Carbon_Fields\VERSION, self::$settings['deps']['carbon_fields'], '<' ) ) {
    $error = '<strong>' . self::$settings['data']['Name'] . ':</strong> ' . __('An unacceptable condition has been detected. An outdated version of Carbon Fields was loaded: ' . Carbon_Fields\VERSION) . ' (&gt;= '.self::$settings['deps']['carbon_fields'] . ' ' . __('required') . ')';
    // ...and also, your favorite band sucks.
  }

  if($error) Utils::show_notice($error, 'error', false);
  return !$error;
}

(Notes: Utils::show_notice() is just my helper function for displaying WordPress admin notices, self::$settings['data']['Name'] contains the "Plugin Name" field as specified in the main plugin PHP file, self::$settings['deps']['carbon_fields'] is a string set to the minimum version of CF that my plugin needs loaded (for this one, '2.0.0', but I have others running '1.6.0' and '1.5.0' that I haven't updated to 2.0.1 yet))

EDIT 2: I know you're using it in a theme, but you could modify as needed. HTH

All 4 comments

Maybe try?

require_once( __DIR__ . '/vendor/autoload.php' );

add_action( 'plugins_loaded', array( 'Carbon_Fields\\Carbon_Fields', 'boot' ) );

Regarding the folder path, you can change it with mnsami/composer-custom-directory-installer. Example:
https://github.com/htmlburger/carbon-fields/issues/273

EDIT: Here is a longer example of what I'm doing...

public function __construct() {
  // Initialize Carbon Fields and verify dependencies
  add_action( 'plugins_loaded', array( 'Carbon_Fields\\Carbon_Fields', 'boot' ) );
  add_action( 'carbon_fields_loaded', array($this, 'load_plugin') );
}

public function load_plugin() {
  if(!$this->verify_dependencies()) return;

  // Enqueue scripts
  new EnqueueScripts();

  // Add admin settings page(s)
  new Settings();

  // Run core plugin logic
  new Core();
}

private function verify_dependencies() {
  // Check if outdated version of Carbon Fields loaded
  $error = false;

  if(!defined('\\Carbon_Fields\\VERSION')) {
    $error = '<strong>' . self::$settings['data']['Name'] . ':</strong> ' . __('A fatal error occurred while trying to load dependencies.');
  } else if( version_compare( Carbon_Fields\VERSION, self::$settings['deps']['carbon_fields'], '<' ) ) {
    $error = '<strong>' . self::$settings['data']['Name'] . ':</strong> ' . __('An unacceptable condition has been detected. An outdated version of Carbon Fields was loaded: ' . Carbon_Fields\VERSION) . ' (&gt;= '.self::$settings['deps']['carbon_fields'] . ' ' . __('required') . ')';
    // ...and also, your favorite band sucks.
  }

  if($error) Utils::show_notice($error, 'error', false);
  return !$error;
}

(Notes: Utils::show_notice() is just my helper function for displaying WordPress admin notices, self::$settings['data']['Name'] contains the "Plugin Name" field as specified in the main plugin PHP file, self::$settings['deps']['carbon_fields'] is a string set to the minimum version of CF that my plugin needs loaded (for this one, '2.0.0', but I have others running '1.6.0' and '1.5.0' that I haven't updated to 2.0.1 yet))

EDIT 2: I know you're using it in a theme, but you could modify as needed. HTH

Fatal error: Uncaught Error: Class 'Carbon_Fields\Carbon_Fields' not found

This is 99% caused by the autoloader not being included. Please make sure your require statement points to the correct autoload.php file path generated by composer.

also, something to note, when installing it from within the theme directory in creates

This is because the package has a type of wordpress-plugin which combined with composer/installer installs it in wp-content/plugins/carbon-fields relative to your composer.json position. This means that when you are installing it from your theme you should add this to your composer.json:

"extra": {
    "installer-paths": {
        "vendor/{$vendor}/{$name}/": ["type:wordpress-plugin"]
    }
}

___Note:___ we may push an update this week that removes the need for this.

got it thanks, the quickstart on https://carbonfields.net/docs/carbon-fields-quickstart/?crb_version=2-0-0 instructs to run composer from within the theme directory.

2.0.2 has been released which changes how Carbon Fields are installed.
The main package is no longer marked as a wordpress-plugin so it will install in the vendor directory where your composer.json is (i.e. the mentioned extra key is no longer needed if you are installing CF in your theme).

The plugin loader file is now it's own composer package:
https://github.com/htmlburger/carbon-fields/releases/tag/2.0.2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Roman52 picture Roman52  路  3Comments

abdusfauzi picture abdusfauzi  路  3Comments

AlexBa picture AlexBa  路  3Comments

halvardos picture halvardos  路  3Comments

jonwaldstein picture jonwaldstein  路  3Comments