Support

Account

Home Forums Backend Issues (wp-admin) Hooking into a field type

Helping

Hooking into a field type

  • I would like to have the option to output the textarea as a list, like the automatically add breaks and autop.

    I have modified the class file class-acf-field-textarea.php

    by adding this at the to the choices array;

    'li' => __("Add li tags"),

    and modifying the function format_value() like this;

    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);
    			
    		} elseif ($field['new_lines'] == 'li') {
    			
    			$value = '<ul><li>'.$value;
    			
    			$value = str_replace(array("\r\n", "\r", "\n"), "</li><li>", $value); 
    			
    			$value .= '</li></ul>';
    			
    			$value = str_replace("<li></li>", "", $value); 
    			
    		}
    		
    		
    		// return
    		return $value;
    	}

    which works nicely but, of course, it will get overwritten by the next update and should be in a filter.

    How is the proper way to do this?

    According to the docs, the first part should be;

    function my_acf_load_field( $field ) {
    
    $field['choices'] = array(
    	'li' => 'Add li tags'
    );
    
    return $field;
    
    }
    
    add_filter('acf/load_field/type=textarea', 'my_acf_load_field');

    but this doesn’t work.

    and the second part I have to override the function in acf/load_values but I don’t know how this filter would work.

    Thanks for any help.

  • the acf/load_field filter will not work on field settings. Basically what you need to do is build a custom field type where you copy the existing field type and add your functionality to it. https://www.advancedcustomfields.com/resources/creating-a-new-field-type/

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Hooking into a field type’ is closed to new replies.