Field groups

This documentation for the Custom post types plugin for WordPress allows you to customize all features.

To get professional support you need to make a donation to get the PRO version of the plugin.

You can add as many custom field groups as you like, fields can be added to: post types, taxonomies, administrative pages.

Unlock and exceed the limits of WordPress and make your website professional in every project.

New field group using UI

Registering and adding a new field group to your WordPress website has never been easier directly from the administration area.

  1. From the “Extend / Manage” menu click on the “Field groups” item;
  2. From the “Field groups” page at the top click on the “Add field group” button;
  3. Configure the new field group and click on the “Publish” button;
  4. See the new field group in the content where it was assigned;

New field group using hook

cpt_register_fields

add_filter('cpt_register_fields', function($fieldGroups){
    $fields = [
        [
            'key' => 'custom-description',
            'label' => 'Custom description',
            'required' => false,
            'info' => 'This is an example field',
            'wrap_width' => 100, // percent
            'wrap_layout' => 'vertical', // vertical - horizontal
            'wrap_class' => 'css-class-name',
            'wrap_id' => 'html-element-id',
            'type' => 'tinymce',
            'extra' => [ // based on type
                'placeholder' => 'Lorem ipsum dolor',
                'autoresize' => true
            ]
        ]
    ];

    $fieldGroups[] = [
        'id' => 'custom-fields-section',
        'label' => 'This is a custom field section',
        'supports' => [
            [
                'type' => 'cpt', // cpt - tax - options
                'id' => 'products',
            ]
        ],
        'position' => 'normal', // normal - side - advanced (only for cpt)
        'order' => 1,
        'admin_only' => false,
        'fields' => $fields,
    ];
    
    return $fieldGroups;
});
This code is an example, add it to theme’s functions.php file or to custom plugin.

Extra by field type

text:
- placeholder: string

number:
- placeholder: string
- min: int
- max: int

textarea:
- placeholder: string
- rows: int
- cols: int

tinymce:
- placeholder: string
- autoresize: boolean

checkbox:
- options: array / string (values separated by '|')

radio:
- options: array / string (values separated by '|')

select:
- placeholder: string
- multiple: boolean
- options: array / string (values separated by '|')

tel:
- placeholder: string

email:
- placeholder: string

date:
- placeholder: string
- min: string (Y-m-d)
- max: string (Y-m-d)

time:
- placeholder: string
- min: string (H:i)
- max: string (H:i)

color:
- alpha: boolean

file:
- types: image / audio / video / application/pdf / application/zip / text/plain / application/msword

post_rel:
- placeholder: string
- multiple: boolean
- post_type: post_type-id (ex 'post')

tax_rel:
- placeholder: string
- multiple: boolean
- taxonomy: taxonomy-id (ex 'category')

html:
- content: string

repeater:
- array (field group args)

Manipulation hook

cpt_{FIELD_TYPE}_field_args

add_filter('cpt_tinymce_field_args', function($args){
    $args['info'] = 'Description for all fields of type "tinymce"';
    return $args;
});
This code is an example, add it to theme’s functions.php file or to custom plugin.

cpt_sanitize_field_{FIELD_KEY}

add_filter('cpt_sanitize_field_custom-description', function($value){
    $value = wp_strip_all_tags($value); // remove all html tags
    return $value;
});
This code is an example, add it to theme’s functions.php file or to custom plugin.

cpt_sanitize_{CONTENT_ID}

add_filter('cpt_sanitize_field_products', function($value){
    $value = wp_strip_all_tags($value); // remove all html tags
    return $value;
});
This code is an example, add it to theme’s functions.php file or to custom plugin.

cpt_sanitize_{CONTENT_ID} _field_{FIELD_KEY}

add_filter('cpt_sanitize_field_products_field_custom-description', function($value){
    $value = wp_strip_all_tags($value); // remove all html tags
    return $value;
});
This code is an example, add it to theme’s functions.php file or to custom plugin.

cpt_get_field_type_{FIELD_TYPE}

add_filter('cpt_get_field_type_tinymce', function($value){
    $value = '<div>' . $value . '</div>'; // add a wrap
    return $value;
});
This code is an example, add it to theme’s functions.php file or to custom plugin.

cpt_get_field_{FIELD_KEY}

add_filter('cpt_get_field_custom-description', function($value){
    $value = '<div>' . $value . '</div>'; // add a wrap
    return $value;
});
This code is an example, add it to theme’s functions.php file or to custom plugin.

Add custom field type

cpt_field_types

add_filter('cpt_field_types', function($fieldTypes){
    $fieldTypes['text'] = [
        'label' => __('Text', 'custom-post-types'),
        'templateCallback' => function ($name, $id, $config) {
            return sprintf(
                '<input type="text" name="%s" id="%s" value="%s" autocomplete="off" aria-autocomplete="none"%s%s>',
                $name,
                $id,
                $config['value'],
                !empty($config['extra']['placeholder']) ? ' placeholder="' . $config['extra']['placeholder'] . '"' : '',
                !empty($config['required']) ? ' required' : ''
            );
        },
        'extra' => [
            [ //placeholder
                'key' => 'placeholder',
                'label' => __('Placeholder', 'custom-post-types'),
                'info' => false,
                'required' => false,
                'type' => 'text',
                'extra' => [],
                'wrap' => [
                    'width' => '',
                    'class' => '',
                    'id' => '',
                    'layout' => ''
                ]
            ],
        ],
        'sanitizeCallback' => function ($value) {
            return sanitize_text_field($value);
        }
    ];
    return $fieldTypes;
});
This code is an example, add it to theme’s functions.php file or to custom plugin.

Get custom field value

from post type

[cpt-field key="custom-description" post-id=""] // by default post-id is current post

// or return by PHP function
cpt_field($key, $post_id);

from taxonomy

[cpt-term-field key="custom-description" term-id="78"]

// or return by PHP function
cpt_term_field($key, $term_id);

from admin page

[cpt-option-field key="custom-description" option-id="custom-options"]

// or return by PHP function
cpt_option_field($key, $option_id);
*** *******