Home Forums ACF PRO Clear fields on save if radio button is set


Clear fields on save if radio button is set

  • I’m using conditional logic to show one of two types of address fields depending on what radio button is selected (US or International Address).

    My concern is someone will start to use the US address field then switch to International or vice versa and then both sets of addresses are saved. Which of course is undesirable.

    Any suggestions on how to cleared the undesired fields on save?


  • Well, it depends on what you consider undesirable. There are two things that you can do, one in the template and one in the admin.

    In the template you can get the value of the radio field and then get the correct address field based on which is selected. This would be my preference since it requires the least amount of extra code.

    In the admin you can create an acf/save_post action and in this function you can see what the value of the radio field is and then delete the contents of the other field.

  • I thought of doing it in the template but I did not want to have extraneous entries in the database.

    I’ll look into option #2 but my guess is that I’ll end up with going the template route.

    Thanks for the reply.

  • It you want to keep the extra data out of the database, then the second way may be the only way.

    However, you might actually want to try it and see what is saved in the database. I thought I read something about data in hidden conditional field not being saved, remember exactly where or when I heard it. No point in doing a lot of work if it is already being taken care of. If this is the case I’m sure that ACF will not delete existing data, for example if the user actually saves the post and then changes their mind and fills in the other field.

  • Very interesting.. it’s true, if the fields are hidden data is not saved.
    however if you had previously saved info in those fields it remains even if they are hidden on the next save.

  • I generally like this feature of ACF, not deleting data. My experience with clients is that when they change their mind several time and go back and forth, they don’t want to have to re-enter the data when they do. They expect that it will be remembered. If ACF checked and deleted it, I’d end up building something to keep it from happening. I don’t think extra data in the database is an issue most of the time.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Clear fields on save if radio button is set’ is closed to new replies.