acf_field_text

The Advanced Custom Fields acf field text class.

Defined (1)

The class is defined in the following location(s).

/core/fields/text.php  
  1. class acf_field_text extends acf_field 
  2.  
  3. /** 
  4. * __construct 
  5. * Set name / label needed for actions / filters 
  6. * @since 3.6 
  7. * @date 23/01/13 
  8. */ 
  9.  
  10. function __construct() 
  11. // vars 
  12. $this->name = 'text'; 
  13. $this->label = __("Text", 'acf'); 
  14. $this->defaults = array( 
  15. 'default_value' => '',  
  16. 'formatting' => 'html',  
  17. 'maxlength' => '',  
  18. 'placeholder' => '',  
  19. 'prepend' => '',  
  20. 'append' => '' 
  21. ); 
  22.  
  23.  
  24. // do not delete! 
  25. parent::__construct(); 
  26.  
  27.  
  28.  
  29. /** 
  30. * create_field() 
  31. * Create the HTML interface for your field 
  32. * @param $field - an array holding all the field's data 
  33. * @type action 
  34. * @since 3.6 
  35. * @date 23/01/13 
  36. */ 
  37.  
  38. function create_field( $field ) 
  39. // vars 
  40. $o = array( 'id', 'class', 'name', 'value', 'placeholder' ); 
  41. $e = ''; 
  42.  
  43.  
  44. // maxlength 
  45. if( $field['maxlength'] !== "" ) 
  46. $o[] = 'maxlength'; 
  47.  
  48.  
  49. // prepend 
  50. if( $field['prepend'] !== "" ) 
  51. $field['class'] .= ' acf-is-prepended'; 
  52. $e .= '<div class="acf-input-prepend">' . $field['prepend'] . '</div>'; 
  53.  
  54.  
  55. // append 
  56. if( $field['append'] !== "" ) 
  57. $field['class'] .= ' acf-is-appended'; 
  58. $e .= '<div class="acf-input-append">' . $field['append'] . '</div>'; 
  59.  
  60.  
  61. $e .= '<div class="acf-input-wrap">'; 
  62. $e .= '<input type="text"'; 
  63.  
  64. foreach( $o as $k ) 
  65. $e .= ' ' . $k . '="' . esc_attr( $field[ $k ] ) . '"';  
  66.  
  67. $e .= ' />'; 
  68. $e .= '</div>'; 
  69.  
  70.  
  71. // return 
  72. echo $e; 
  73.  
  74.  
  75. /** 
  76. * create_options() 
  77. * Create extra options for your field. This is rendered when editing a field. 
  78. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  79. * @param $field - an array holding all the field's data 
  80. * @type action 
  81. * @since 3.6 
  82. * @date 23/01/13 
  83. */ 
  84.  
  85. function create_options( $field ) 
  86. // vars 
  87. $key = $field['name']; 
  88.  
  89. ?> 
  90. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  91. <td class="label"> 
  92. <label><?php _e("Default Value", 'acf'); ?></label> 
  93. <p><?php _e("Appears when creating a new post", 'acf') ?></p> 
  94. </td> 
  95. <td> 
  96. <?php  
  97. do_action('acf/create_field', array( 
  98. 'type' => 'text',  
  99. 'name' => 'fields[' .$key.'][default_value]',  
  100. 'value' => $field['default_value'],  
  101. )); 
  102. ?> 
  103. </td> 
  104. </tr> 
  105. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  106. <td class="label"> 
  107. <label><?php _e("Placeholder Text", 'acf'); ?></label> 
  108. <p><?php _e("Appears within the input", 'acf') ?></p> 
  109. </td> 
  110. <td> 
  111. <?php  
  112. do_action('acf/create_field', array( 
  113. 'type' => 'text',  
  114. 'name' => 'fields[' .$key.'][placeholder]',  
  115. 'value' => $field['placeholder'],  
  116. )); 
  117. ?> 
  118. </td> 
  119. </tr> 
  120. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  121. <td class="label"> 
  122. <label><?php _e("Prepend", 'acf'); ?></label> 
  123. <p><?php _e("Appears before the input", 'acf') ?></p> 
  124. </td> 
  125. <td> 
  126. <?php  
  127. do_action('acf/create_field', array( 
  128. 'type' => 'text',  
  129. 'name' => 'fields[' .$key.'][prepend]',  
  130. 'value' => $field['prepend'],  
  131. )); 
  132. ?> 
  133. </td> 
  134. </tr> 
  135. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  136. <td class="label"> 
  137. <label><?php _e("Append", 'acf'); ?></label> 
  138. <p><?php _e("Appears after the input", 'acf') ?></p> 
  139. </td> 
  140. <td> 
  141. <?php  
  142. do_action('acf/create_field', array( 
  143. 'type' => 'text',  
  144. 'name' => 'fields[' .$key.'][append]',  
  145. 'value' => $field['append'],  
  146. )); 
  147. ?> 
  148. </td> 
  149. </tr> 
  150. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  151. <td class="label"> 
  152. <label><?php _e("Formatting", 'acf'); ?></label> 
  153. <p><?php _e("Affects value on front end", 'acf') ?></p> 
  154. </td> 
  155. <td> 
  156. <?php  
  157. do_action('acf/create_field', array( 
  158. 'type' => 'select',  
  159. 'name' => 'fields['.$key.'][formatting]',  
  160. 'value' => $field['formatting'],  
  161. 'choices' => array( 
  162. 'none' => __("No formatting", 'acf'),  
  163. 'html' => __("Convert HTML into tags", 'acf') 
  164. )); 
  165. ?> 
  166. </td> 
  167. </tr> 
  168. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  169. <td class="label"> 
  170. <label><?php _e("Character Limit", 'acf'); ?></label> 
  171. <p><?php _e("Leave blank for no limit", 'acf') ?></p> 
  172. </td> 
  173. <td> 
  174. <?php  
  175. do_action('acf/create_field', array( 
  176. 'type' => 'number',  
  177. 'name' => 'fields[' .$key.'][maxlength]',  
  178. 'value' => $field['maxlength'],  
  179. )); 
  180. ?> 
  181. </td> 
  182. </tr> 
  183. <?php 
  184.  
  185.  
  186.  
  187. /** 
  188. * format_value() 
  189. * This filter is appied to the $value after it is loaded from the db and before it is passed to the create_field action 
  190. * @type filter 
  191. * @since 3.6 
  192. * @date 23/01/13 
  193. * @param $value - the value which was loaded from the database 
  194. * @param $post_id - the $post_id from which the value was loaded 
  195. * @param $field - the field array holding all the field options 
  196. * @return $value - the modified value 
  197. */ 
  198.  
  199. function format_value( $value, $post_id, $field ) 
  200. $value = htmlspecialchars($value, ENT_QUOTES); 
  201.  
  202. return $value; 
  203.  
  204.  
  205. /** 
  206. * format_value_for_api() 
  207. * This filter is appied to the $value after it is loaded from the db and before it is passed back to the api functions such as the_field 
  208. * @type filter 
  209. * @since 3.6 
  210. * @date 23/01/13 
  211. * @param $value - the value which was loaded from the database 
  212. * @param $post_id - the $post_id from which the value was loaded 
  213. * @param $field - the field array holding all the field options 
  214. * @return $value - the modified value 
  215. */ 
  216.  
  217. function format_value_for_api( $value, $post_id, $field ) 
  218. // validate type 
  219. if( !is_string($value) ) 
  220. return $value; 
  221.  
  222.  
  223. if( $field['formatting'] == 'none' ) 
  224. $value = htmlspecialchars($value, ENT_QUOTES); 
  225. elseif( $field['formatting'] == 'html' ) 
  226. $value = nl2br($value); 
  227.  
  228.  
  229. return $value; 
  230.