Typo on this line
if (empty($field('value'])) {
should be
if (empty($field['value'])) {
Can’t see typos easily here when adding code. There are other typos as well, I did not always spell references correctly. Sorry about these, I was just typing out a quick example and sometimes it happens.
All of the code should go in functions.php. Automating things can sometimes be difficult. You have to way the time it takes to do the automation vs how much time it will save.