Support

Account

Home Forums ACF PRO Image not saving as URL

Solved

Image not saving as URL

  • Another problem with the image upload field.

    I have selected it to store as a URL, but images uploaded are being saved to the database as the ID, not a URL.

    I tried changing it to array, still being saved as ID. I tried changing it back to URL, still saved as ID.

    In between each test I went into the database and deleted the key-value to be sure it wasn’t just hanging onto something.

    I’m using it on the User Profile page so that authors can upload a profile photo, but having trouble getting it to display in the bio area because it’s just echoing the image ID, not the URL.

    Any help will be most gratefully appreciated!

  • Well, I have it working, but not the way I’d like it to, and there is still a bug in ACF Pro (current version).

    The only way I can get it to work is to change how the image is stored to as an ID.

    The bug is that regardless of how I select it to be stored (my preference would be as a URL), it stores it as an ID anyway – so changing it to Array or URL has no effect – it still just shows up in the database with an ID number.

    BUT regardless of how it is stored, unless I change that setting to *also* be “ID” then I can’t use it using the get_field function.

    The ONLY way I can get it to work is to have it set to store as an ID and use the echo wp_get_attachment_image( $imgID, $size ); method, which is not ideal because then WP adds classes and styling that I don’t want and have to override with my own styles, and I am limited to the WP image sizes (although I know I can add my own and have, but in some places I just want to add a one-off image size without having to put it in my functions first) .

    If I could just echo the URL, I can structure my own image link using my own sizes and styles and pull in whatever other author meta I want….

    Any help would still be welcomed, ideally I’d like to see the bug fixed in a future update – if the option exists to store images as an array or as a URL, it should save to the database using the selected option.

  • ACF has always saved all image fields as the image ID. When selecting ID, Array or Object you are only selecting the value returned when using function to get the values. See this page for using the different values returned by image fields https://www.advancedcustomfields.com/resources/image/

  • Hi John,
    Thanks for the quick reply. I did use that page as a resource, and tried all the methods listed – the only one that works is wp_get_attachment_image.

    The other methods, using basic object, customized display object, and URL, did not work.

    Also, in the current version of ACF Pro, the choices now are “Image Array”, “Image URL” or “Image ID” there is no longer an “Image Object” choice when creating a field.

    SO if I select “Image Array” and use the Object methods (basic or customized) it doesn’t display any image, it doesn’t recognize that there is a value stored at all in fact (my fallback Gravatar displays instead since I have an if(!$image) statement in case no image is uploaded).

    If I select URL and use the URL method, instead of echoing the URL in the image tag, it echos the ID number, so no image displays.

  • Can you post some of the code that’s not working for you?

  • Gah!!…..now it’s working. Sigh. I *swear* it was not working earlier, LOL. But I have been fiddling with this all day today….changing the setting from Array to URL to ID and back again, changing my code calling the image, etc. Maybe it’s been too long of a day.

    Very sorry for wasting your time but I really do appreciate your willingness to help, many thanks.

  • It’s never a waste of time 🙂 Glad you got it working. I have those kind of days myself. Sometimes it just takes stepping away for a minute which taking the time to post here provided the opportunity to do.

  • Thank you for being understanding……I really do try to work things out pretty exhaustively before I post for help…..I just hate asking for help until I really need it, but you’re right – sometimes I should just step away for a bit and work on something different, this was a good reminder. 🙂

    I’m going to mark your last post as ‘this solved..” because it was your request for me to post my code that had me looking at it again…

  • How did you get it to work exactly? This is my current problem where I’m getting the ID not the URL. I’m doing something VERY similar to you in Users.

  • Hi Darrenbachan,

    Here is what I’m doing, I’ve commented it below for the benefit of anyone else who might need this:

    <?php 
    $author_id = get_the_author_meta('ID');// Needed because we are outside of the Loop
    $image = get_field('author_profile_picture', 'user_'. $author_id );
    
    if ( !( $image ) ) {
     echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'quark_author_bio_avatar_size', 125 ) ); // Defaults to Gravatar if no image is uploaded, the 'quark' filters is a theme function that sets the size, if not using Quark Theme you don't need that bit
    
    } else { ?>
      <img src="<?php echo $image; ?>" width="124" height="148" alt="<?php echo $author_id->nicename; ?>" />
    <?php } ?>

    My problem was that I wasn’t specifying the author by ID in order for my template to get the right image…..once I figured that out it worked.

    You CAN still use wp_get_attachment_image( $imgID, $size ); and that method works well, it’s just that it uses some core function to add classes and styling and image sizes – great if you don’t have more specific needs.

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

You must be logged in to reply to this topic.