07788 994423 ยท contact@soliddigital.io

ACF Take the contents of a text area, de-dupe, remove blanks and sort list.

If you have a list of companies, materials, names or any list which one you constantly updating. This simple function will help you remove duplicates and remove blanks and sort list in alphabetical order.

text_list name of your ACF Text Area field. ACF New Lines set to Automatically add paragraphs.

/**
* Take the contents of a text area, de-dupe, remove blanks and sort.
*
* @param string $string Text area content.
*
* @return string
*/
function tidy_textarea_lines( $string ) {
$source_lines = explode( “\n”, str_replace( “\r”, ”, $string ) );
$source_lines = array_map( ‘trim’, $source_lines ); // Trim white space from lines.
$source_lines = array_unique( array_filter( $source_lines ) ); // Remove blanks & duplicates.
sort( $source_lines );
return implode( PHP_EOL, $source_lines );
}
add_filter( ‘acf/update_value/name=text_list‘, ‘tidy_textarea_lines’, 10, 1 );