• Resolved signsi

    (@signsi)


    Hi all,

    Is it possible to use a textarea instead of the text field in combination with Meta Box’s key value field? I am aware of the size parameter. But this does no help with multi-line data.

    Thanks and regards

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support longnguyen

    (@longnguyen)

    Hi,

    Yes, it is possible. We can create a new field type, extends the class RWMB_Key_Value_Field which render the field key_value and change the default input type=text for value field to textarea.

    – Create a new field type custom_key_value:

    if ( class_exists( 'RWMB_Field' ) ) {
        class RWMB_Custom_Key_Value_Field extends RWMB_Key_Value_Field {
            public static function html( $meta, $field ) {
                // Key.
                $key                       = isset( $meta[0] ) ? $meta[0] : '';
                $attributes                = self::get_attributes( $field, $key );
                $attributes['placeholder'] = $field['placeholder']['key'];
                $html                      = sprintf( '<input %s>', self::render_attributes( $attributes ) );
    
                // Value.
                $val                       = isset( $meta[1] ) ? $meta[1] : '';
                $attributes                = self::get_attributes( $field, $val );
                $attributes['placeholder'] = $field['placeholder']['value'];
                $html                     .= sprintf( '<textarea %s>' . $attributes['value'] . '</textarea>', self::render_attributes( $attributes ) );
    
                return $html;
            }
        }
    }

    – Use the new field type:

    add_filter( 'rwmb_meta_boxes', 'custom_key_value_field' );
    function custom_key_value_field( $meta_boxes ){
        $meta_boxes[] = array(
            'title'      => 'Custom Key Value Meta Box',
            'post_type' => 'post',
            'id' => 'custom_key_value_meta_box',
            'fields' => array(
                array(
                    'name' => 'Custom Key Value Field',
                    'id'   => 'custom_key_value_field',
                    'type' => 'custom_key_value',
                )
            ),
        );
        return $meta_boxes;
    }

    For more information, please follow the documentation:
    https://docs.metabox.io/custom-field-type/
    https://github.com/wpmetabox/meta-box/blob/master/inc/fields/key-value.php

    Thread Starter signsi

    (@signsi)

    Thanks! Works like a charm.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Key Value field with textarea’ is closed to new replies.