Home › Forums › Front-end Issues › Tag field with auto-creation of new tags › Reply To: Tag field with auto-creation of new tags
If this was a must have item for one of my clients, what I would do is use some type of ACF field. This could be a multi-select field that you’re using. I would more than likely leave it as it is so for those that want to take the time to use it they can.
Then I would create another field. I would use a text field or maybe a textarea. I would name it something like “Add Hobbies” Instructions “Don’t see the hobbies your looking for? Add New Ones” or something like that. In this field they could enter either a comma separated list or alternately a list with each item on a new line if using a text area.
Then I would create an ACF save post filter https://www.advancedcustomfields.com/resources/acf-save_post/.
In this filter I would see if the user entered anything in this field. If they did I would parse the value of the field to get a list of new tags. I would double check this list to make sure the tags did not already exist.
Then for the ones I would create them https://developer.wordpress.org/reference/functions/wp_insert_term/.
Then I would append these terms to the post using https://developer.wordpress.org/reference/functions/wp_set_object_terms/ with $append set to true.
Finally I would delete the value of this new field.
It’s a bit of work on your end, but I think it would achieve the results you’re looking for.
—
Alternately you can eliminate the field your using and just use a text field and do what I suggest. Hoever, this would be more prone to errors with people adding alternate spellings of existing tags.
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.