carbon-fields
carbon-fields copied to clipboard
How to boot Carbon Fields with my plugin?
I am trying to boot Carbon Fields from my WordPress plugin, but I get this error:
Carbon Fields must be booted before the "init" WordPress action has fired
How do I fix this? This is not a specific use-case. I need to run Carbon only when my plugin is activated, otherwise not.
If I understand you correctly, it's right in the docs.
Something like this:
use Carbon_Fields\Container;
use Carbon_Fields\Field;
add_action( 'plugins_loaded', function(){
add_action( 'carbon_fields_register_fields', __NAMESPACE__ . '\crb_attach_theme_options' );
add_action( 'after_setup_theme', __NAMESPACE__ . '\crb_load' );
})
function crb_attach_theme_options() {
Container::make( 'theme_options', __( 'Theme Options', 'crb' ) )
->add_fields( array(
Field::make( 'text', 'crb_text', 'Text Field' ),
) );
}
function crb_load() {
require_once( 'vendor/autoload.php' );
\Carbon_Fields\Carbon_Fields::boot();
}
@MikeiLL
How can I do add_action( 'carbon_fields_register_fields', 'crb_attach_theme_options' );
inside WordPress init hook. For example:
add_action( 'init', function(){ add_action( 'carbon_fields_register_fields', 'crb_attach_theme_options' ); })
If you're doing it in a plugin, I think you want to do it at admin_init
or maybe plugins_loaded
but not sure. What error are you seeing when you run the above code?
In updates on this? Would love to use it in my plugin, but seems like it is more trouble than it is worth.
I am using it within my plugin without issues, simply using the plugins_loaded hook to do
require_once( 'vendor/autoload.php' );
\Carbon_Fields\Carbon_Fields::boot();
Awesome to hear @tamara-m . Did that also work with the get function CF has and/or a shortcode?
@thezacharytaylor not adding a shortcode from this particular plugin, but I do save and get saved theme_options normally.
I'm loading this from a namespaced class, FWIW
If you need help let me know? Ideally sharing all your code, I'm sure we can figure it out.
I am using it within my plugin without issues, simply using the plugins_loaded hook to do require_once( 'vendor/autoload.php' ); \Carbon_Fields\Carbon_Fields::boot();
Can you share the full snippet please?
I came across this today on a setup and now use the following:
`// Require once the Composer Autoload. if ( file_exists( dirname( FILE ) . '/lib/autoload.php' ) ) { require_once dirname( FILE ) . '/lib/autoload.php';
}
//Added the CRB loader here to avoid fatal errors on latest PHP versions function crb_load() { \Carbon_Fields\Carbon_Fields::boot(); } add_action( 'after_setup_theme', NAMESPACE . '\crb_load' );`