name = 'text_area'; $this->label = __( 'Text Area', 'frontend-admin' ); $this->public = false; $this->defaults = array( 'default_value' => '', 'new_lines' => '', 'maxlength' => '', 'placeholder' => '', 'rows' => '', ); // 'textarea' is handled by the centralized character-counter hooks in Field_Base, // so it's excluded here to avoid rendering the "Show Remaining Characters" setting twice. $fields = ['post_excerpt', 'product_short_description']; foreach ( $fields as $field ) { add_filter( "acf/prepare_field/type={$field}", [ $this, 'prepare_field' ], 10, 1 ); add_filter( "acf/render_field_settings/type={$field}", [ $this, 'remaining_characters_setting' ], 10, 1 ); } } function prepare_field( $field ) { if( empty( $field['maxlength'] ) || empty( $field['show_remaining_characters'] ) ) return $field; // Add custom attributes $field['wrapper']['data-char-remaining'] = 1; $field['wrapper']['data-remaining-text'] = $field['remaining_characters_text']; $field['wrapper']['data-maxlength'] = $field['maxlength']; return $field; } function remaining_characters_setting( $field ) { Field_Base::render_remaining_characters_fields( $field ); } /* * render_field() * * Create the HTML interface for your field * * @param $field - an array holding all the field's data * * @type action * @since 3.6 * @date 23/01/13 */ function render_field( $field ) { // vars $atts = array(); $keys = array( 'id', 'class', 'name', 'value', 'placeholder', 'rows', 'maxlength' ); $keys2 = array( 'readonly', 'disabled', 'required' ); // rows if ( ! $field['rows'] ) { $field['rows'] = 8; } // atts (value="123") foreach ( $keys as $k ) { if ( isset( $field[ $k ] ) ) { $atts[ $k ] = $field[ $k ]; } } // atts2 (disabled="disabled") foreach ( $keys2 as $k ) { if ( ! empty( $field[ $k ] ) ) { $atts[ $k ] = $k; } } // remove empty atts $atts = acf_clean_atts( $atts ); // return acf_textarea_input( $atts ); } /* * render_field_interactive() * * Create the interactive HTML interface for your field that uses data-wp-* attributes. * This is the new way of rendering fields and will eventually replace render_field() in most cases. * * @param $field - an array holding all the field's data * @type action * @since 5.7.0 * @date 2024-06-10 */ function render_field_interactive( $field ) { $field = wp_parse_args( $field, [ 'prepend' => '', 'append' => '', 'rows' => 8, 'type' => $this->name, 'class' => '', 'input_data' => [], ] ); $class = $field['class'] ?? ''; if ( $field['prepend'] !== '' ) { $class .= ' acf-is-prepended'; } if ( $field['append'] !== '' ) { $class .= ' acf-is-appended'; } $input_attrs = [ 'type' => $field['type'] ?? $this->name, 'class' => $class ]; $attr_keys = [ 'id', 'value', 'placeholder', 'maxlength', 'pattern', 'readonly', 'disabled', 'required' ]; if ( ! empty( $field['no_autocomplete'] ) ) { $input_attrs['autocomplete'] = 'off'; } if ( ! empty( $field['input_data'] ) ) { foreach ( $field['input_data'] as $k => $data ) { $input_attrs[ 'data-' . $k ] = $data; } } foreach ( $attr_keys as $k ) { if ( isset( $field[ $k ] ) ) { $input_attrs[ $k ] = $field[ $k ]; } } // base field name stored in context $field_name = $field['name'] ?? ''; $input_attrs['data-wp-bind--name'] = "callbacks.getFieldName"; ?>
__( 'Default Value', 'frontend-admin' ), 'instructions' => __( 'Appears when creating a new post', 'frontend-admin' ), 'type' => 'textarea', 'name' => 'default_value', ) ); } /** * Renders the field settings used in the "Validation" tab. * * @since 6.0 * * @param array $field The field settings array. * @return void */ function render_field_validation_settings( $field ) { acf_render_field_setting( $field, array( 'label' => __( 'Character Limit', 'frontend-admin' ), 'instructions' => __( 'Leave blank for no limit', 'frontend-admin' ), 'type' => 'number', 'name' => 'maxlength', ) ); } /** * Renders the field settings used in the "Presentation" tab. * * @since 6.0 * * @param array $field The field settings array. * @return void */ function render_field_presentation_settings( $field ) { acf_render_field_setting( $field, array( 'label' => __( 'Rows', 'frontend-admin' ), 'instructions' => __( 'Sets the textarea height', 'frontend-admin' ), 'type' => 'number', 'name' => 'rows', 'placeholder' => 8, ) ); acf_render_field_setting( $field, array( 'label' => __( 'Placeholder Text', 'frontend-admin' ), 'instructions' => __( 'Appears within the input', 'frontend-admin' ), 'type' => 'text', 'name' => 'placeholder', ) ); acf_render_field_setting( $field, array( 'label' => __( 'New Lines', 'frontend-admin' ), 'instructions' => __( 'Controls how new lines are rendered', 'frontend-admin' ), 'type' => 'select', 'name' => 'new_lines', 'choices' => array( 'wpautop' => __( 'Automatically add paragraphs', 'frontend-admin' ), 'br' => __( 'Automatically add <br>', 'frontend-admin' ), '' => __( 'No Formatting', 'frontend-admin' ), ), ) ); } /* * format_value() * * This filter is applied to the $value after it is loaded from the db and before it is returned to the template * * @type filter * @since 3.6 * @date 23/01/13 * * @param $value (mixed) the value which was loaded from the database * @param $post_id (mixed) the $post_id from which the value was loaded * @param $field (array) the field array holding all the field options * * @return $value (mixed) the modified value */ function format_value( $value, $post_id, $field ) { // bail early if no value or not for template if ( empty( $value ) || ! is_string( $value ) ) { return $value; } // new lines if ( $field['new_lines'] == 'wpautop' ) { $value = wpautop( $value ); } elseif ( $field['new_lines'] == 'br' ) { $value = nl2br( $value ); } // return return $value; } /** * validate_value * * Validates a field's value. * * @date 29/1/19 * @since 5.7.11 * * @param (bool|string) Whether the value is vaid or not. * @param mixed $value The field value. * @param array $field The field array. * @param string $input The HTML input name. * @return (bool|string) */ function validate_value( $valid, $value, $field, $input ) { // Check maxlength. if ( ! empty( $field['maxlength'] ) && ( acf_strlen( $value ) > $field['maxlength'] ) ) { return sprintf( /* translators: %d: number */ __( 'Value must not exceed %d characters', 'frontend-admin' ), $field['maxlength'] ); } // Return. return $valid; } /** * Return the schema array for the REST API. * * @param array $field * @return array */ function get_rest_schema( array $field ) { $schema = parent::get_rest_schema( $field ); if ( ! empty( $field['maxlength'] ) ) { $schema['maxLength'] = (int) $field['maxlength']; } return $schema; } } endif; // class_exists check