class-acf-field-accordion.php
5 months ago
class-acf-field-button-group.php
5 months ago
class-acf-field-checkbox.php
2 months ago
class-acf-field-color_picker.php
5 months ago
class-acf-field-date_picker.php
5 months ago
class-acf-field-date_time_picker.php
5 months ago
class-acf-field-email.php
5 months ago
class-acf-field-file.php
5 months ago
class-acf-field-google-map.php
5 months ago
class-acf-field-group.php
5 months ago
class-acf-field-icon_picker.php
6 months ago
class-acf-field-image.php
4 weeks ago
class-acf-field-link.php
5 months ago
class-acf-field-message.php
6 months ago
class-acf-field-number.php
5 months ago
class-acf-field-oembed.php
2 months ago
class-acf-field-output.php
6 months ago
class-acf-field-page_link.php
4 weeks ago
class-acf-field-password.php
5 months ago
class-acf-field-post_object.php
4 weeks ago
class-acf-field-radio.php
2 months ago
class-acf-field-range.php
5 months ago
class-acf-field-relationship.php
4 weeks ago
class-acf-field-select.php
2 months ago
class-acf-field-separator.php
6 months ago
class-acf-field-tab.php
6 months ago
class-acf-field-taxonomy.php
2 months ago
class-acf-field-text.php
5 months ago
class-acf-field-textarea.php
5 months ago
class-acf-field-time_picker.php
5 months ago
class-acf-field-true_false.php
5 months ago
class-acf-field-url.php
5 months ago
class-acf-field-user.php
2 months ago
class-acf-field-wysiwyg.php
5 months ago
class-acf-field.php
5 months ago
index.php
2 years ago
class-acf-field-text.php
229 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package ACF |
| 4 | * @author WP Engine |
| 5 | * |
| 6 | * © 2026 Advanced Custom Fields (ACF®). All rights reserved. |
| 7 | * "ACF" is a trademark of WP Engine. |
| 8 | * Licensed under the GNU General Public License v2 or later. |
| 9 | * https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | */ |
| 11 | |
| 12 | if ( ! class_exists( 'acf_field_text' ) ) : |
| 13 | |
| 14 | class acf_field_text extends acf_field { |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * This function will setup the field type data |
| 19 | * |
| 20 | * @type function |
| 21 | * @date 5/03/2014 |
| 22 | * @since 5.0.0 |
| 23 | * |
| 24 | * @param n/a |
| 25 | * @return n/a |
| 26 | */ |
| 27 | function initialize() { |
| 28 | |
| 29 | // vars |
| 30 | $this->name = 'text'; |
| 31 | $this->label = __( 'Text', 'acf' ); |
| 32 | $this->description = __( 'A basic text input, useful for storing single string values.', 'acf' ); |
| 33 | $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-text.png'; |
| 34 | $this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/text/', 'docs', 'field-type-selection' ); |
| 35 | $this->defaults = array( |
| 36 | 'default_value' => '', |
| 37 | 'maxlength' => '', |
| 38 | 'placeholder' => '', |
| 39 | 'prepend' => '', |
| 40 | 'append' => '', |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Create the HTML interface for your field |
| 47 | * |
| 48 | * @param $field - an array holding all the field's data |
| 49 | * |
| 50 | * @type action |
| 51 | * @since 3.6 |
| 52 | * @date 23/01/13 |
| 53 | */ |
| 54 | function render_field( $field ) { |
| 55 | $html = ''; |
| 56 | |
| 57 | // Prepend text. |
| 58 | if ( $field['prepend'] !== '' ) { |
| 59 | $field['class'] .= ' acf-is-prepended'; |
| 60 | $html .= '<div class="acf-input-prepend">' . acf_esc_html( $field['prepend'] ) . '</div>'; |
| 61 | } |
| 62 | |
| 63 | // Append text. |
| 64 | if ( $field['append'] !== '' ) { |
| 65 | $field['class'] .= ' acf-is-appended'; |
| 66 | $html .= '<div class="acf-input-append">' . acf_esc_html( $field['append'] ) . '</div>'; |
| 67 | } |
| 68 | |
| 69 | // Input. |
| 70 | $input_attrs = array(); |
| 71 | foreach ( array( 'type', 'id', 'class', 'name', 'value', 'placeholder', 'maxlength', 'pattern', 'readonly', 'disabled', 'required' ) as $k ) { |
| 72 | if ( isset( $field[ $k ] ) ) { |
| 73 | $input_attrs[ $k ] = $field[ $k ]; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if ( isset( $field['input-data'] ) && is_array( $field['input-data'] ) ) { |
| 78 | foreach ( $field['input-data'] as $name => $attr ) { |
| 79 | $input_attrs[ 'data-' . $name ] = $attr; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $html .= '<div class="acf-input-wrap">' . acf_get_text_input( acf_filter_attrs( $input_attrs ) ) . '</div>'; |
| 84 | |
| 85 | // Display. |
| 86 | echo $html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- only safe HTML output generated and escaped by functions above. |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Create extra options for your field. This is rendered when editing a field. |
| 92 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 93 | * |
| 94 | * @param $field - an array holding all the field's data |
| 95 | * |
| 96 | * @type action |
| 97 | * @since 3.6 |
| 98 | * @date 23/01/13 |
| 99 | */ |
| 100 | function render_field_settings( $field ) { |
| 101 | acf_render_field_setting( |
| 102 | $field, |
| 103 | array( |
| 104 | 'label' => __( 'Default Value', 'acf' ), |
| 105 | 'instructions' => __( 'Appears when creating a new post', 'acf' ), |
| 106 | 'type' => 'text', |
| 107 | 'name' => 'default_value', |
| 108 | ) |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Renders the field settings used in the "Validation" tab. |
| 114 | * |
| 115 | * @since 6.0 |
| 116 | * |
| 117 | * @param array $field The field settings array. |
| 118 | * @return void |
| 119 | */ |
| 120 | function render_field_validation_settings( $field ) { |
| 121 | acf_render_field_setting( |
| 122 | $field, |
| 123 | array( |
| 124 | 'label' => __( 'Character Limit', 'acf' ), |
| 125 | 'instructions' => __( 'Leave blank for no limit', 'acf' ), |
| 126 | 'type' => 'number', |
| 127 | 'name' => 'maxlength', |
| 128 | ) |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Renders the field settings used in the "Presentation" tab. |
| 134 | * |
| 135 | * @since 6.0 |
| 136 | * |
| 137 | * @param array $field The field settings array. |
| 138 | * @return void |
| 139 | */ |
| 140 | function render_field_presentation_settings( $field ) { |
| 141 | acf_render_field_setting( |
| 142 | $field, |
| 143 | array( |
| 144 | 'label' => __( 'Placeholder Text', 'acf' ), |
| 145 | 'instructions' => __( 'Appears within the input', 'acf' ), |
| 146 | 'type' => 'text', |
| 147 | 'name' => 'placeholder', |
| 148 | ) |
| 149 | ); |
| 150 | |
| 151 | acf_render_field_setting( |
| 152 | $field, |
| 153 | array( |
| 154 | 'label' => __( 'Prepend', 'acf' ), |
| 155 | 'instructions' => __( 'Appears before the input', 'acf' ), |
| 156 | 'type' => 'text', |
| 157 | 'name' => 'prepend', |
| 158 | ) |
| 159 | ); |
| 160 | |
| 161 | acf_render_field_setting( |
| 162 | $field, |
| 163 | array( |
| 164 | 'label' => __( 'Append', 'acf' ), |
| 165 | 'instructions' => __( 'Appears after the input', 'acf' ), |
| 166 | 'type' => 'text', |
| 167 | 'name' => 'append', |
| 168 | ) |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * validate_value |
| 174 | * |
| 175 | * Validates a field's value. |
| 176 | * |
| 177 | * @date 29/1/19 |
| 178 | * @since 5.7.11 |
| 179 | * |
| 180 | * @param (bool|string) Whether the value is vaid or not. |
| 181 | * @param mixed $value The field value. |
| 182 | * @param array $field The field array. |
| 183 | * @param string $input The HTML input name. |
| 184 | * @return (bool|string) |
| 185 | */ |
| 186 | function validate_value( $valid, $value, $field, $input ) { |
| 187 | |
| 188 | // Check maxlength |
| 189 | if ( $field['maxlength'] && ( acf_strlen( $value ) > $field['maxlength'] ) ) { |
| 190 | return sprintf( __( 'Value must not exceed %d characters', 'acf' ), $field['maxlength'] ); |
| 191 | } |
| 192 | |
| 193 | // Return. |
| 194 | return $valid; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Return the schema array for the REST API. |
| 199 | * |
| 200 | * @param array $field |
| 201 | * @return array |
| 202 | */ |
| 203 | function get_rest_schema( array $field ) { |
| 204 | $schema = parent::get_rest_schema( $field ); |
| 205 | |
| 206 | if ( ! empty( $field['maxlength'] ) ) { |
| 207 | $schema['maxLength'] = (int) $field['maxlength']; |
| 208 | } |
| 209 | |
| 210 | return $schema; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Returns an array of JSON-LD Property output types that are supported by this field type. |
| 215 | * |
| 216 | * @since 6.8 |
| 217 | * |
| 218 | * @return string[] |
| 219 | */ |
| 220 | public function get_jsonld_output_types(): array { |
| 221 | return array( 'Text', 'URL' ); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | |
| 226 | // initialize |
| 227 | acf_register_field_type( 'acf_field_text' ); |
| 228 | endif; // class_exists check |
| 229 |