Hi,
oftentimes many common users doesn’t see invisible characters like space or newline. When content must be design perfect it would be fine to have an option in every custom field to trim content.
Or is there some solution how to do this?
Hi @radovan.smitala
TO have this option built into the core would bloat the plugin.
You can easily do this via code like so:
echo trim( get_field('field_name') );
Thank you for reply!
I know that i could trim it on output, it is good only for website.
I use WordPress with custom fields as App with REST API with WP-API module or JSON-API module for another platforms like mobile or facebook app.
And there is neccesary to have consistent data for every device.
Maybe i could use some Filter hooks to trim all fields.
Hi @radovan.smitala
Yes, there are filters run on the value which you can read about over on the docs page.
Thanks
E
Who wants same feature, there is solution:
put this to functions.php
function trim_acf_fields( $post_id )
{
// load from post
if( isset($_POST['fields']) ) {
foreach($_POST['fields'] as $key => $value) {
$_POST['fields'][$key] = trim($value);
}
}
}
// run before ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'trim_acf_fields', 1);
Sorry for replying to a 2 year old post. I agree that trimming fields should be included in the plugin. But the code posted by Radovan didn’t worked for me, since I also have checkboxes (in the end the ACF values aren’t strings but an array, and former code fails).
Here is a slightly improved code for anyone stumbling on this page
function trim_acf_fields( $post_id )
{
// load from post
if( isset($_POST['fields']) ) {
foreach($_POST['fields'] as $key => $value) {
if(!is_array($value))
$_POST['fields'][$key] = trim($value);
}
}
}
// run before ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'trim_acf_fields', 1);