Home › Forums › Feature Requests › Option to trim custom field
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);
The topic ‘Option to trim custom field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.