htmlburger / carbon-fields

WordPress Custom Fields Library ✨
https://carbonfields.net/
Other
1.38k stars 245 forks source link

How to boot Carbon Fields with my plugin? #997

Open rehanadil opened 3 years ago

rehanadil commented 3 years ago

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.

MikeiLL commented 3 years ago

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();
}
rehanadil commented 3 years ago

@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' ); })

MikeiLL commented 3 years ago

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?

thezacharytaylor commented 2 years ago

In updates on this? Would love to use it in my plugin, but seems like it is more trouble than it is worth.

tamara-m commented 2 years ago

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();

thezacharytaylor commented 2 years ago

Awesome to hear @tamara-m . Did that also work with the get function CF has and/or a shortcode?

tamara-m commented 2 years ago

@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.

kcrtr commented 1 year ago

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?

mrkdevelopment commented 1 year ago

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' );`