Home Forums General Issues modify field on post save


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.


    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.

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

You must be logged in to reply to this topic.