I am trying to update an image field inside a user form with within an acf/pre_save_post filter:
update_field( $imagefield, $image_id, “user_” . $user_id );
The problem is that the field does not get updated. I tried replacing the image field by a text field for a user and it works. I also tried updating an image field inside a post and it also works.
Could you try to replicate to see if it works on your side?
I am actually uploading an image from an url like this
$create_image = new JDN_Create_Media_File( $image_url );
$image_id = $create_image->attachment_id;
JDN_Create_Media_File is from here.
I am now updating the image of the user with the acf/save_post with the name of the field and it works.
I tried using it with acf/pre_save_post it did not work. Maybe it is because the image is not uploaded quickly enough? The strange thing is that using the update_field function with an image field inside a post worked (with name and with key field). I also tried updating a field for a user and it also worked (with name and with key field), it gave me the correct image ID.
Thanks for you help.
Do you have 2 fields? one to upload and image and the second to include the image from a URL? If you do my guess would be the image field itself is being overwritten with no value if an image is not uploaded at some point after you have set it in the acf/pre_save_post filter. But that’s just a guess assuming that you have two fields to deal with different ways to supply an image.
Now that you say it it makes sense.
I have a field for an url and if the url is from a specific domain it gets an image url with file_get_contents().
Then I have an image field thats shows with conditional statement.
What would be the way to set an order for the fields to be saved if I need the image field to appear after the url field? Is it possible? If not it is not a big deal as I am now using with acf/save_post.
Well, if you have conditional logic set for the two fields so that only one of them is shown then the value for the image field should not be submitted at all. So I can’t say why this would be happening. The only reason that both fields would be submitted is if both fields are visible, or at least that’s the way it’s supposed to work.
ACF saves values in the order that they’re submitted, which is usually the order that they appear in on the page.
The topic ‘Problem with update_field for a user image field’ is closed to new replies.
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!