Home › Forums › Backend Issues (wp-admin) › Set default value for all empty fields on save › Reply To: Set default value for all empty fields on save
Ok, well I’ve got something that works now. But as I am a newbie to ACF snippets, I would be very grateful if someone would check this over and let me know how I might improve it. Thanks!
/* Fill empty ACF fields with "blank" so TablePress can hide them */
function set_empty_value( $post_id ) {
if ( 'product' != get_post_type($post_id) ) {
return;
}
$fields = get_fields();
if( $fields ) {
foreach ( $fields as $name => $value ) {
if ( $value == '' ) {
$value = "blank";
update_field( $name, $value );
}
}
}
}
add_action('acf/save_post', 'set_empty_value', 10, 1 );
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.