Home › Forums › Backend Issues (wp-admin) › Found root cause for fields not saving (using ACF PRO 5.0.8) › Reply To: Found root cause for fields not saving (using ACF PRO 5.0.8)
After some tests, I realize that in group fields, ACF create fields named “field_***” (used for post_name) but after saving post_name become “field-***”.
The reason is that wp_insert_post() (used for creating field) sanitize post_name with sanitize_title function (check wp-includes/post.php on line 3189).
To prevent this, I have to add a filter to return original post_name.
In advanced-custom-fields-pro/api/api-field.php, I add in function acf_update_field() on line 839 :
add_filter( 'sanitize_title', 'acf_update_field_sanitize_title', 100, 3 );
just before :
$field['ID'] = wp_insert_post( $save );
and remove that filter just after saving :
remove_filter( 'sanitize_title', 'acf_update_field_sanitize_title', 100 );
And after acf_update_field :
function acf_update_field_sanitize_title ( $title, $raw_title, $context ) {
return $raw_title;
}
It seems to work for me…
Note :
if you don’t want to alter ACF sources, you can add this to your functions.php :
function acf_update_field_sanitize_title ( $title, $raw_title, $context ) {
return $raw_title;
}
// add filter for sanitize_title
add_filter( 'acf/update_field', function ( $field ) {
add_filter( 'sanitize_title', 'acf_update_field_sanitize_title', 100, 3 );
return $field;
}, 10, 1 );
// remove filter for sanitize_title
add_filter( 'wp_unique_post_slug', function ( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
remove_filter( 'sanitize_title', 'acf_update_field_sanitize_title', 100 );
return $slug;
}, 10, 6 );
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.