Support

Account

Home Forums General Issues Add Image Size to another ACF Field

Solving

Add Image Size to another ACF Field

  • I am uploading an image through the image field.

    Is it possible to pass a smaller size version of the image automatically to another ACF image field using a web hook or function?

    many thanks

  • Hi @neodjandre

    I just want to let you know that you can get the smaller image (thumbnail, for example) by passing the size to the array. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/image/.

    If you still want to pass a smaller size version of the image to another image field, then you can try to insert the file by using the wp_insert_attachment() function, get the image ID, and then update the new field by using the update_field() function like this:

    // You need to get the post ID where the new custom field is located.
    $post_id = 123;
    
    // This is a dummy image ID. You should get it by using the
    // wp_insert_attachment() function
    $image_id = 99;
    
    // Set the new field key. We should use the field key to making sure that
    // the data is saved correctly
    $field_key = 'field_1234567890abc';
    
    // Update the field
    update_field($field_key, $image_id, $post_id);

    Also, please keep in mind that you can just update the new field with the original image ID instead. Because ACF saves the image field as the image ID in the database, it won’t need a lot of space. After that, you can just get the smaller size by passing the size to the array.

    I hope this helps 🙂

  • thanks, I am trying to figure out how to incorporate your suggestion on my existing code which is this but I can’t figure it out!

    function acf_set_featured_image( $value, $post_id, $field  ){
    
        if($value != ''){
          //Add the value which is the image ID to the _thumbnail_id meta data for the current post
          add_post_meta($post_id, '_thumbnail_id', $value);
        }
    
        return $value;
    }
    
    // acf/update_value/name={$field_name} - filter for a specific field based on it's name
    add_filter('acf/update_value/name=fl_image', 'acf_set_featured_image', 10, 3);

    any help would be much appreciated!

  • Hi @neodjandre

    You should be able to do it by using the acf/save_post hook like this:

    function my_acf_save_post( $post_id ) {
        
        // get image id
        $image_id = get_field('fl_image', $post_id, false);
        
        // set the thumbnail field key
        $thumbnail_field_key = 'field_1234567890abc';
        
        // set the value of the thumbnail field
        update_field($thumbnail_field_key, $image_id, $post_id);
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    Where field_1234567890abc is the field key you want to update. After that, you can get the value like this:

    $thumb_image = get_field(thumbnail_id);
    $thumb_url = $thumb_image['sizes']['thumbnail'];

    If you are using a URL custom field to store the URL, you should be able to do it like this:

    function my_acf_save_post( $post_id ) {
        
        // get image id
        $image = get_field('fl_image', $post_id);
        
        // set the thumbnail URL field key
        $thumbnail_url_field_key = 'field_1234567890abc';
        
        // set the value of the thumbnail URL field
        update_field($thumbnail_url_field_key, $image['sizes']['thumbnail'], $post_id);
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    Then you can get the URL like this:

    $thumb_image = get_field(thumbnail_id);

    I hope this helps 🙂

  • Hi thanks for this,

    I am using

    function my_acf_save_post( $post_id ) {
    
        // get image id
        $image = get_field('fl_image', $post_id);
    
        // set the thumbnail URL field key
        $thumbnail_url_field_key = 'fl_afeat';
    
        // set the value of the thumbnail URL field
        update_field($thumbnail_url_field_key, $image['sizes']['medium'], $post_id);
    
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    but nothing happens when I click on “Update Post”.

    Both fields: fl_image and fl_afeat are image fields with the Image URL option set.

    What would be wrong?

  • Hi @neodjandre

    Please keep in mind that the second method needs the fl_afeat field to be a URL field, not an image field.

    If you really need the thumbnail to be an individual image object, you need to upload and process it first. Unfortunately, this is out of ACF topic. Kindly check this page to learn more about it: http://wordpress.stackexchange.com/questions/141172/how-can-i-pass-an-image-file-to-wp-handle-upload. If you need further support regarding the upload issue, kindly visit WordPress community instead.

    I hope this makes sense 🙂

  • Hi,

    I don’t see an option to choose a URL field, only Text, Email, Password.

    When I first upload an image to fl_image, wordpress automatically generates the ‘medium’ version of the image.

    For example, at the moment I upload an image to fl_image with name:

    duck.jpg

    the version created for ‘medium’ is named:

    duck-300×169.jpg

    I can’t seem to pass this ‘medium’ file, neither as a URL nor as an image object to an ACF field, with any of the code above..

  • Hi @neodjandre

    I’m sorry I thought you were using the PRO version. In this case, you should be able to use the text field instead.

    Like I said before, If you need the medium image to be an individual image object in the media library, you need to upload it to your site programmatically. Unfortunately, this kind of topic is more related to WordPress and PHP.

    Also, could you please let me know what’s wrong with calling the image like this:

    $image = get_field('fl_image');
    echo $image['sizes']['medium'];

    I think it’s simpler than what you were trying to do and it will save spaces because it won’t create redundant images.

    Thanks 🙂

  • Hi,

    I just upgraded to PRO now. However, fl_image returns only Image URL, not Image Array.

    I suppose if I change this to Image Array, I could get the medium size as you are suggesting. However, I didn’t want to do that in the first place because the array contains unnecessary information.

  • Hi @neodjandre

    Yes, it will only show the image URL, and yes, by using the image array method, you will get other data too. That’s why I told you to use the original field instead of creating a new one.

    What you are trying to do is uploading the resized image programmatically. This needs advanced PHP code and advanced knowledge on how WordPress uploads the files. I believe you can do it, but it is so much easier to save the resized file to your computer and upload it manually than creating the code you need, especially if you are not familiar with WordPress and PHP.

    To do that you need to upload the file again somehow using WordPress core functions and then get the attachment ID. After that, you can update the new field with the new attachment by using the update_field() function

    If you don’t know how to do it and have no time to learn about it, I suggest you hire a developer to help you out with it, and I’d recommend looking for one on https://studio.envato.com/, https://www.upwork.com/, or https://codeable.io/.

    I hope this makes sense 🙂

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

The topic ‘Add Image Size to another ACF Field’ is closed to new replies.