acf_field_wysiwyg

The Advanced Custom Fields acf field wysiwyg class.

Defined (1)

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

/core/fields/wysiwyg.php  
  1. class acf_field_wysiwyg 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 = 'wysiwyg'; 
  13. $this->label = __("Wysiwyg Editor", 'acf'); 
  14. $this->category = __("Content", 'acf'); 
  15. $this->defaults = array( 
  16. 'toolbar' => 'full',  
  17. 'media_upload' => 'yes',  
  18. 'default_value' => '',  
  19. ); 
  20.  
  21.  
  22. // Create an acf version of the_content filter (acf_the_content) 
  23. if( isset($GLOBALS['wp_embed']) ) { 
  24.  
  25. add_filter( 'acf_the_content', array( $GLOBALS['wp_embed'], 'run_shortcode' ), 8 ); 
  26. add_filter( 'acf_the_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 ); 
  27.  
  28.  
  29. add_filter( 'acf_the_content', 'capital_P_dangit', 11 ); 
  30. add_filter( 'acf_the_content', 'wptexturize' ); 
  31. add_filter( 'acf_the_content', 'convert_smilies' ); 
  32. add_filter( 'acf_the_content', 'convert_chars' ); 
  33. add_filter( 'acf_the_content', 'wpautop' ); 
  34. add_filter( 'acf_the_content', 'shortcode_unautop' ); 
  35. //add_filter( 'acf_the_content', 'prepend_attachment' ); *should only be for the_content (causes double image on attachment page) 
  36. add_filter( 'acf_the_content', 'do_shortcode', 11); 
  37.  
  38.  
  39. // do not delete! 
  40. parent::__construct(); 
  41.  
  42.  
  43. // filters 
  44. add_filter( 'acf/fields/wysiwyg/toolbars', array( $this, 'toolbars'), 1, 1 ); 
  45. add_filter( 'mce_external_plugins', array( $this, 'mce_external_plugins'), 20, 1 ); 
  46.  
  47.  
  48.  
  49. /** 
  50. * mce_external_plugins 
  51. * This filter will add in the tinyMCE 'code' plugin which is missing in WP 3.9 
  52. * @type function 
  53. * @date 18/04/2014 
  54. * @since 5.0.0 
  55. * @param $post_id (int) 
  56. * @return $post_id (int) 
  57. */ 
  58.  
  59. function mce_external_plugins( $plugins ) { 
  60.  
  61. // global 
  62. global $wp_version; 
  63.  
  64.  
  65. // WP 3.9 an above 
  66. if( version_compare($wp_version, '3.9', '>=' ) ) { 
  67.  
  68. // add code 
  69. $plugins['code'] = apply_filters('acf/get_info', 'dir') . 'js/tinymce.code.min.js'; 
  70.  
  71.  
  72.  
  73. // return 
  74. return $plugins; 
  75.  
  76.  
  77.  
  78. /** 
  79. * toolbars() 
  80. * This filter allowsyou to customize the WYSIWYG toolbars 
  81. * @param $toolbars - an array of toolbars 
  82. * @return $toolbars - the modified $toolbars 
  83. * @type filter 
  84. * @since 3.6 
  85. * @date 23/01/13 
  86. */ 
  87.  
  88. function toolbars( $toolbars ) { 
  89.  
  90. // global 
  91. global $wp_version; 
  92.  
  93.  
  94. // vars 
  95. $editor_id = 'acf_settings'; 
  96.  
  97.  
  98. if( version_compare($wp_version, '3.9', '>=' ) ) { 
  99.  
  100. // Full 
  101. $toolbars['Full'] = array( 
  102.  
  103. 1 => apply_filters( 'mce_buttons', array('bold', 'italic', 'strikethrough', 'bullist', 'numlist', 'blockquote', 'hr', 'alignleft', 'aligncenter', 'alignright', 'link', 'unlink', 'wp_more', 'spellchecker', 'fullscreen', 'wp_adv' ), $editor_id ),  
  104.  
  105. 2 => apply_filters( 'mce_buttons_2', array( 'formatselect', 'underline', 'alignjustify', 'forecolor', 'pastetext', 'removeformat', 'charmap', 'outdent', 'indent', 'undo', 'redo', 'wp_help', 'code' ), $editor_id ),  
  106.  
  107. 3 => apply_filters('mce_buttons_3', array(), $editor_id),  
  108.  
  109. 4 => apply_filters('mce_buttons_4', array(), $editor_id),  
  110.  
  111. ); 
  112.  
  113.  
  114. // Basic 
  115. $toolbars['Basic'] = array( 
  116.  
  117. 1 => apply_filters( 'teeny_mce_buttons', array('bold', 'italic', 'underline', 'blockquote', 'strikethrough', 'bullist', 'numlist', 'alignleft', 'aligncenter', 'alignright', 'undo', 'redo', 'link', 'unlink', 'fullscreen'), $editor_id ),  
  118.  
  119. ); 
  120.  
  121. } else { 
  122.  
  123. // Full 
  124. $toolbars['Full'] = array( 
  125.  
  126. 1 => apply_filters( 'mce_buttons', array('bold', 'italic', 'strikethrough', 'bullist', 'numlist', 'blockquote', 'justifyleft', 'justifycenter', 'justifyright', 'link', 'unlink', 'wp_more', 'spellchecker', 'fullscreen', 'wp_adv' ), $editor_id ),  
  127.  
  128. 2 => apply_filters( 'mce_buttons_2', array( 'formatselect', 'underline', 'justifyfull', 'forecolor', 'pastetext', 'pasteword', 'removeformat', 'charmap', 'outdent', 'indent', 'undo', 'redo', 'wp_help', 'code' ), $editor_id ),  
  129.  
  130. 3 => apply_filters('mce_buttons_3', array(), $editor_id),  
  131.  
  132. 4 => apply_filters('mce_buttons_4', array(), $editor_id),  
  133.  
  134. ); 
  135.  
  136.  
  137. // Basic 
  138. $toolbars['Basic'] = array( 
  139.  
  140. 1 => apply_filters( 'teeny_mce_buttons', array('bold', 'italic', 'underline', 'blockquote', 'strikethrough', 'bullist', 'numlist', 'justifyleft', 'justifycenter', 'justifyright', 'undo', 'redo', 'link', 'unlink', 'fullscreen'), $editor_id ),  
  141.  
  142. ); 
  143.  
  144.  
  145.  
  146.  
  147. // Custom - can be added with acf/fields/wysiwyg/toolbars filter 
  148.  
  149.  
  150. return $toolbars; 
  151.  
  152.  
  153. /** 
  154. * input_admin_head() 
  155. * This action is called in the admin_head action on the edit screen where your field is created. 
  156. * Use this action to add css and javascript to assist your create_field() action. 
  157. * @info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_head 
  158. * @type action 
  159. * @since 3.6 
  160. * @date 23/01/13 
  161. */ 
  162.  
  163. function input_admin_head() 
  164. add_action( 'admin_footer', array( $this, 'admin_footer') ); 
  165.  
  166. function admin_footer() 
  167. ?> 
  168. <div style="display:none;"> 
  169. <?php wp_editor( '', 'acf_settings' ); ?> 
  170. </div> 
  171. <?php 
  172.  
  173.  
  174. /** 
  175. * create_field() 
  176. * Create the HTML interface for your field 
  177. * @param $field - an array holding all the field's data 
  178. * @type action 
  179. * @since 3.6 
  180. * @date 23/01/13 
  181. */ 
  182.  
  183. function create_field( $field ) { 
  184.  
  185. // global 
  186. global $wp_version; 
  187.  
  188.  
  189. // vars 
  190. $id = uniqid('acf-editor-'); 
  191. $default_editor = 'tinymce'; 
  192.  
  193.  
  194. // filter value for editor 
  195. remove_filter( 'acf_the_editor_content', 'format_for_editor', 10, 2 ); 
  196. remove_filter( 'acf_the_editor_content', 'wp_htmledit_pre', 10, 1 ); 
  197. remove_filter( 'acf_the_editor_content', 'wp_richedit_pre', 10, 1 ); 
  198.  
  199.  
  200. // WP 4.3 
  201. if( version_compare($wp_version, '4.3', '>=' ) ) { 
  202.  
  203. add_filter( 'acf_the_editor_content', 'format_for_editor', 10, 2 ); 
  204.  
  205.  
  206. // WP < 4.3 
  207. } else { 
  208.  
  209. $function = user_can_richedit() ? 'wp_richedit_pre' : 'wp_htmledit_pre'; 
  210.  
  211. add_filter('acf_the_editor_content', $function, 10, 1); 
  212.  
  213.  
  214.  
  215. // filter 
  216. $field['value'] = apply_filters( 'acf_the_editor_content', $field['value'], $default_editor ); 
  217.  
  218. ?> 
  219. <div id="wp-<?php echo $id; ?>-wrap" class="acf_wysiwyg wp-core-ui wp-editor-wrap tmce-active" data-toolbar="<?php echo $field['toolbar']; ?>" data-upload="<?php echo $field['media_upload']; ?>"> 
  220. <div id="wp-<?php echo $id; ?>-editor-tools" class="wp-editor-tools hide-if-no-js"> 
  221. <?php if( user_can_richedit() && $field['media_upload'] == 'yes' ): ?> 
  222. <div id="wp-<?php echo $id; ?>-media-buttons" class="wp-media-buttons"> 
  223. <?php do_action( 'media_buttons', $id ); ?> 
  224. </div> 
  225. <?php endif; ?> 
  226. </div> 
  227. <div id="wp-<?php echo $id; ?>-editor-container" class="wp-editor-container"> 
  228. <textarea id="<?php echo $id; ?>" class="wp-editor-area" name="<?php echo $field['name']; ?>"><?php echo $field['value']; ?></textarea> 
  229. </div> 
  230. </div> 
  231. <?php 
  232.  
  233.  
  234.  
  235. /** 
  236. * create_options() 
  237. * Create extra options for your field. This is rendered when editing a field. 
  238. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  239. * @type action 
  240. * @since 3.6 
  241. * @date 23/01/13 
  242. * @param $field - an array holding all the field's data 
  243. */ 
  244.  
  245. function create_options( $field ) 
  246. // vars 
  247. $key = $field['name']; 
  248.  
  249. ?> 
  250. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  251. <td class="label"> 
  252. <label><?php _e("Default Value", 'acf'); ?></label> 
  253. <p><?php _e("Appears when creating a new post", 'acf') ?></p> 
  254. </td> 
  255. <td> 
  256. <?php  
  257. do_action('acf/create_field', array( 
  258. 'type' => 'textarea',  
  259. 'name' => 'fields['.$key.'][default_value]',  
  260. 'value' => $field['default_value'],  
  261. )); 
  262. ?> 
  263. </td> 
  264. </tr> 
  265. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  266. <td class="label"> 
  267. <label><?php _e("Toolbar", 'acf'); ?></label> 
  268. </td> 
  269. <td> 
  270. <?php 
  271.  
  272. $toolbars = apply_filters( 'acf/fields/wysiwyg/toolbars', array() ); 
  273. $choices = array(); 
  274.  
  275. if( is_array($toolbars) ) 
  276. foreach( $toolbars as $k => $v ) 
  277. $label = $k; 
  278. $name = sanitize_title( $label ); 
  279. $name = str_replace('-', '_', $name); 
  280.  
  281. $choices[ $name ] = $label; 
  282.  
  283. do_action('acf/create_field', array( 
  284. 'type' => 'radio',  
  285. 'name' => 'fields['.$key.'][toolbar]',  
  286. 'value' => $field['toolbar'],  
  287. 'layout' => 'horizontal',  
  288. 'choices' => $choices 
  289. )); 
  290. ?> 
  291. </td> 
  292. </tr> 
  293. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  294. <td class="label"> 
  295. <label><?php _e("Show Media Upload Buttons?", 'acf'); ?></label> 
  296. </td> 
  297. <td> 
  298. <?php  
  299. do_action('acf/create_field', array( 
  300. 'type' => 'radio',  
  301. 'name' => 'fields['.$key.'][media_upload]',  
  302. 'value' => $field['media_upload'],  
  303. 'layout' => 'horizontal',  
  304. 'choices' => array( 
  305. 'yes' => __("Yes", 'acf'),  
  306. 'no' => __("No", 'acf'),  
  307. )); 
  308. ?> 
  309. </td> 
  310. </tr> 
  311. <?php 
  312.  
  313.  
  314. /** 
  315. * format_value_for_api() 
  316. * 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 
  317. * @type filter 
  318. * @since 3.6 
  319. * @date 23/01/13 
  320. * @param $value - the value which was loaded from the database 
  321. * @param $post_id - the $post_id from which the value was loaded 
  322. * @param $field - the field array holding all the field options 
  323. * @return $value - the modified value 
  324. */ 
  325.  
  326. function format_value_for_api( $value, $post_id, $field ) 
  327. // apply filters 
  328. $value = apply_filters( 'acf_the_content', $value ); 
  329.  
  330.  
  331. // follow the_content function in /wp-includes/post-template.php 
  332. $value = str_replace(']]>', ']]>', $value); 
  333.  
  334.  
  335. return $value; 
  336.