I have a snippet to create bullet point for text area field.
However I was told I should use:
return $value
and not
echo
for anything in the acf/format_value
hook. How can I make it in return $value
format?
<?php
function my_acf_format_value( $value, $post_id, $field ) {
$my_acf_format_value = explode("\n", $value);
echo '<ol>';
echo '<li>' . implode( '</li><li>', $my_acf_format_value) . '</li>';
echo '</ol>';
}
add_filter('acf/format_value/name=pt_materials', 'my_acf_format_value', 10, 3);
Instead of using multiple echo
statements, you can use string concatenation (.=
) to join the strings together. Then at the end of the function, you can use the return
statement to pass the value back to WordPress as required.
<?php
function my_acf_format_value( $value, $post_id, $field ) {
$my_acf_format_value = explode("\n", $value);
// Create string variable to append our data to
$return_string = '';
// Append values to our string variable.
$return_string .= '<ol>';
$return_string .= '<li>' . implode( '</li><li>', $my_acf_format_value) . '</li>';
$return_string .= '</ol>';
// Return the value
return $return_string;
}
add_filter('acf/format_value/name=pt_materials', 'my_acf_format_value', 10, 3);
wordpress
I've added additional comments to explain the steps involved, but since this is such a simple function, this could be done with a single line of code:
return '<ol><li>' . implode( '</li><li>', $my_acf_format_value) . '</li></ol>';