lp__add_field()

The lp__add_field function is used to register a new field in your plugin or system. Fields can be of various types, and the data field type, in particular, allows you to display information without user interaction. It is versatile, capable of rendering different types of data such as text, numbers, dates, images, and more.


Plugin


Namespace

Launchpad


Parameters

string

$id

(Required) A unique identifier for the field. This ID will be used for referencing and rendering the field.

array

$args

(Optional) An array of arguments that define the field’s attributes. These may include:

  • label (string): The label that will appear next to the field.
  • type (string): The type of the field. Supported field types include:
    • checkbox
    • clone
    • color-picker
    • data (special field type for displaying data in various formats)
    • date
    • datetime
    • gallery
    • group
    • image
    • message
    • month
    • number
    • password
    • post
    • radio
    • repeater
    • select
    • tab
    • taxonomy
    • text
    • textarea
    • time
    • true-false
    • url
    • wysiwyg
  • default (mixed): Default value for the field.
  • description (string): A short description or helper text.
  • options (array): An array of options for fields like select, radio, etc.
  • data_type (string): (For data fields) Specify the type of data to be rendered. Supported data types include:
    • text
    • number
    • date
    • true_false (for rendering boolean values)
    • image
    • gallery
  • format (string): (For data fields) Optional formatting for data types like date, number, etc.

Source

File: \includes\lp-field-functions.php

/**
  * lp__add_field()
  * 
  * Function to add field. 
  * 
  * Fields that are already exists will not be registered again
  * 
  * @since   1.0.0
  * @uses    lp__field_exists() to check field existence before registrations.
  * @uses    lp__get_field_type_model() to get the field type class model that will 
  *          be use to create new instance of the field initialize it.
  * @uses    lp__register_field() to register and store the field
  * @param   array $args     Field arguments
  * @return  LP_Field instance
  */ 
function lp__add_field( $args ) {
        $id = lp__array_key_value( 'id', $args, '' );
        $field_type = lp__array_key_value( 'type', $args, '' );
        $parent = lp__array_key_value( 'parent', $args, '' );

        if( $id && !lp__field_exists( $id ) ){

            $Field_Model = lp__get_field_type_model( $field_type );
            if( class_exists( $Field_Model ) ){
                $args['instance'] = new $Field_Model( $args );

                // Register the field group
                lp__register_field( $id, $args );
            }

        }

        return lp__array_key_value( 'instance', $args );

}

Example

lp__add_field( 'record_data', array(
    'label' => 'Record Information',
    'type' => 'data',
    'data_type' => 'date',
    'format' => 'F j, Y',
    'description' => 'Displays the record creation date.',
));

In this example, a data field is registered to display a record’s creation date, formatted as Month Day, Year (e.g., October 14, 2024).

Hooks:

You can customize the field arguments using the following filters:

  • lp__field_args: Filter for all fields.
  • lp__field_args/{id}: Filter for a specific field by its ID.

Related

Use to log error messages with control to include call traces and log on page.

Used to dump variable contents into the error log for debugging.