Support

Account

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 );