Home › Forums › General Issues › modify field on post save
Hi,
I have a field that a user will put a YouTube video link in. i would like to have one of two things happen when the post gets saved.
the url would look something like the following: youtubedomain/watch?v=”videoid”&ab_channel=”channelname”
The part that is needed is only the Video id all the other stuff is not needed.
Option 1: remove any extra content before and after the section of the URL that is actually needed.
or
Option 2: have a hidden filed that when a user saves the post with a URL in the first field it could grab only the partition of the URL that’s needed
would this be possible to happen? if so what would an example look like.
You’re URL is going to be different dependent on if they use the “URL” from the browser bar or the URL from the “Share” feature on youtube. Parsing out the id will require accounting for all possibilities.
A URL field must have a valid URL, only a text field would work for holding just the video ID, so you cannot modify a URL field to hold just the video ID. If you did this then then the field may be blank when the admin reloads and if not it would create a validation error on the next save.
Updating one field based on another field is as simple as using an acf/save_post action that gets the value from the URL field, parses out the video id and then use update_field() to update the other field.
I would not do this, instead I would create and acf/format_value filter with a priority > 10 to run after ACF and I would parse out the video id in and return it in this filter.
Sorry when I said url I didn’t actually mean that I was using the field tip url. We are using just a text field.
I will look at that guild to night and see if I can get something working.
Thank you for the information. I will let youbknow if I have any issues.
If you want to modify the value that is saved you would still do this using an acf/save_post action. You would just get the field and then update it with the altered value.
You could skip the update part if you use a priority of < 10 for your acf/save_post action to happen before ACF saves the value. In this case you would modify the value held in $_POST[‘acf’][FIELD_KEY
] and let ACF do the updating.
You must be logged in to reply to this topic.
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.