Support

Account

Home Forums Bug Reports Problem with update_field for a user image field

Solved

Problem with update_field for a user image field

  • Hello,

    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?

  • is $imagefield set to the field name or the field key? If you’re using the field name change it to the field key.

  • Hello,

    Thanks for your message. I tried with the key field and it does not work either.
    However if I update the image field within acf/save_post filter it works. Could it be that update image field does not work with acf/pre_save_post filter?

    Thanks

  • It should work in the acf/pre_save_post filter when using the field key. How are you getting the value for $image_id

  • 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.
    https://joshuadnelson.com/programmatically-add-images-to-media-library/

    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.

    Thanks

  • 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.

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

You must be logged in to reply to this topic.