Carbon-fields does't show maked fields

1.4k Views Asked by At

I create container for theme options and add fields array to it. Then open this options page, see the title and empty block where the fields should be add. How to display fields?

Carbon Fields 2.1.0

use Carbon_Fields\Container;
use Carbon_Fields\Field;
Container::make( 'theme_options', __( 'Theme Options', 'crb' ) )
  ->add_fields( array(
      Field::make( 'text', 'crb_text', 'Text Field' ),
  ) );

enter image description here

2

There are 2 best solutions below

3
On

You can add the code on functions.php and Use this code in below,

use Carbon_Fields\Field;
use Carbon_Fields\Container;

add_action( 'carbon_fields_register_fields', 'crb_attach_theme_options' );
function crb_attach_theme_options() {
    Container::make( 'theme_options', __( 'Theme Options', 'crb' ) )
        ->add_fields( array(
            Field::make( 'rich_text', 'crb_footer_copyright', 'Copyright' ),
        ) );
}

2
On

In order to get Carbon Fields to work in a Plugin, I had to define the value of Carbon_Fields\URL to fix it.

My bootstrap code at the root file of my plugin looks like so:

// Define the url where Carbon Fields assets will be enqueued from
define( 'Carbon_Fields\URL', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'vendor/htmlburger/carbon-fields/' );

// Boot Carbon Fields
\Carbon_Fields\Carbon_Fields::boot();

// Return the class that sets up the settings pages
return new \MyPlugin\Settings();

You can see all the Carbon Field constants that are possible to override before bootstrap here: https://github.com/htmlburger/carbon-fields/blob/development/config.php