How to Use Carbon Fields to Create Custom Fields in WordPress Theme Development
Carbon Fields is a lightweight and flexible WordPress library used to create custom fields for post types, taxonomies, options pages, widgets, and more. It can be especially useful in custom theme development where you need custom meta fields or settings without needing to build everything from scratch. Below is a step-by-step guide on how to integrate and use Carbon Fields in your WordPress custom theme development.
Create a plugin folder in WordPress Plugin Directory.
After creating the plugin folder, run the command below. For load CarbonField Package
composer require htmlburger/carbon-fields
Create a PHP file in the plugin folder, then load the autoload.php and utilities.php files from the include folder, which you should create inside the plugin folder.
<?php
/*
* Plugin Name: Products Fields
* Description: Add Prodcuts Details
* Version: 1.0
* Requires PHP: 7.2
* Author: Rajvinder Singh
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class ProductsFields{
public function __construct(){
require_once ( plugin_dir_path( __FILE__ ).'vendor/autoload.php');
require ( plugin_dir_path( __FILE__ ).'includes/utilities.php' );
}
}
new ProductsFields;
?>
Add the code below in the utilities.php file.
<?php
use Carbon_Fields\Container;
use Carbon_Fields\Field;
add_action( 'carbon_fields_register_fields', 'crb_attach_theme_options' );
function crb_attach_theme_options() {
Container::make( 'post_meta', __( 'Add Products Details' ) )
->where( 'post_type', '=', 'products' )
->add_fields( array(
Field::make( 'checkbox', 'crb_show_content', 'Product Avaliable' ),
Field::make( 'text', 'crb_name', 'Product Name' ),
Field::make( 'text', 'crb_price', 'Product Price' ),
Field::make( 'textarea', 'crb_description', 'Product Description' ),
Field::make( 'media_gallery', 'crb_media_gallery', __( 'Media Gallery' ) )
->set_type( array( 'image' ) ),
) );
}
add_action( 'after_setup_theme', 'crb_load' );
function crb_load() {
\Carbon_Fields\Carbon_Fields::boot();
}
?>
Using Plugin
Once add code in PHP file, activate it. Once activated, Carbon Fields will be available to use in your theme.
After adding custom fields to the custom post type, fetch the data in the PHP template.
Fetch Pricing
$ <?php echo carbon_get_the_post_meta( 'crb_price' ); ?>
Fetch Description
<?php echo carbon_get_the_post_meta( 'crb_description' ); ?>
Checkbox condition of Create Carbon field
<?php if(carbon_get_the_post_meta( 'crb_show_content' ) == 1) :?>
<div class="product_available_single">Product Avalible</div>
<?php else : ?>
<div class="product_not_available_single">Product Not Avalible</div>
<?php endif ;?>
Fetch Gallery Upload images
<?php
$gallery_images = carbon_get_the_post_meta( 'crb_media_gallery' );
foreach($gallery_images as $image){ ?>
<div class="col-md-4">
<div class="gallery_thum">
<img src="<?php echo wp_get_attachment_url( $image ); ?>" class="img-fluid">
</div>
</div>
<?php }
?>