Support

Account

Home Forums Bug Reports Image Format in Rest API is always ID

Solved

Image Format in Rest API is always ID

  • Updated to 5.9.1 and now all images are only output as ID even though it is set to be URLs.

  • Hey, I’ve got the same issue. As it breaks my clients site completely, it’s quite urgent.

    Thanks for having a look,
    Tobi

  • Hey @shortee, I just found a thread from late 2021 which explains the fix for this:
    https://support.advancedcustomfields.com/forums/topic/acf-rest-api-image-only-shows-attachment-id/

    Add ?acf_format=standard to your API call. Hope that fixed your issue too.

    Cheers,
    Tobi

  • thx a lot @dev-plural. this also solved my problem.

  • Very interesting. This also solves my problem, but as the return value for all fields change it creates new problems with other fields.

    Is it possible to change the output format for a single field?

  • Another solution is to use acf/rest/format_value_for_rest/type=image filter

    function custom_acf_rest_api_image_format($value, $post_id, $field) {
        // Get the full image object from the ID using wp_get_attachment_image_src()
        $image = wp_get_attachment_image_src($value, 'full');
        // Return an array with the image URL and other metadata
        return array(
            'url' => $image[0],
            'width' => $image[1],
            'height' => $image[2],
            'alt' => get_post_meta($value, '_wp_attachment_image_alt', true)
        );
    }
    
    // Hook the filter function to the acf/rest/format_value_for_rest/type=image filter
    add_filter('acf/rest/format_value_for_rest/type=image', 'custom_acf_rest_api_image_format', 10, 3);
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.