mywebsos/carbon-field-extended-select

An extension for Carbon Fields that adds a Select field with the ability to retrieve either the label, value, or key/value array.

dev-main 2023-01-17 11:28 UTC

This package is auto-updated.

Last update: 2024-04-17 13:05:19 UTC


README

This extension adds a Select field to the Carbon Fields library, with the added ability to retrieve either the label, value, or key/value array of the selected option.

Installation

To install the extension, you can use the following command:

composer require mywebsos/carbon-fields-extended-select

Usage

To use the Extended Select field in your Carbon Fields container, you can use the following code:

use Carbon_Fields\Container;
use Carbon_Fields\Field;

Container::make( 'post_meta', __( 'Extended Select Field' ) )
    ->add_fields( array(
        Field::make( 'extended_select', 'extended_select_field', __( 'Select options' ) )
            ->set_options( array(
                'option_1' => 'Option 1',
                'option_2' => 'Option 2',
                'option_3' => 'Option 3',
            ) )
            ->return_format( 'array' ) // label, array or value ( default )
    ) );

You can retrieve the selected option using the following methods:

// Retrieve the label|array|value of the selected option
$value = carbon_get_post_meta( get_the_ID(), $field_name );

Note

Make sure to include the use statement of Carbon_Fields\Field at the top of your file.