Home › Forums › Backend Issues (wp-admin) › Image filter for restapi
I need customize restapi. I have function for filtering image data:
function jk_acf_restapi_filtering_image_format($value, $post_id, $field) {
// Ensure that the ACF image field value is valid and represents an existing attachment
if (empty($value) || !wp_attachment_is_image($value)) {
return false; // Return false or an empty array if the value is invalid
}
// Get the full image object from the ID using wp_get_attachment_image_src()
$image = wp_get_attachment_image_src($value, 'full');
// Ensure that the image exists
if (!$image || is_wp_error($image)) {
return false; // Return false or an empty array if the image doesn't exist
}
// Get the attachment post object using get_post()
$attachment = get_post($value);
// Store the base URL of your WordPress site
$base_url = get_home_url();
// Create an array to store the image data with "sizes" key
$data = array(
'url' => str_replace($base_url . '/wp-content/uploads/', '', $image[0]),
'sizes' => array(),
);
// Add the alt text and title
$data['alt'] = get_post_meta($value, '_wp_attachment_image_alt', true);
$data['title'] = $attachment->post_title;
// Loop through each available image size
$metadata = wp_get_attachment_metadata($value);
if (!empty($metadata['sizes'])) {
foreach ($metadata['sizes'] as $size => $details) {
// Get the image URL for the current size using wp_get_attachment_image_src()
$image_src = wp_get_attachment_image_src($value, $size);
// Ensure that the image URL exists for the current size
if ($image_src && !is_wp_error($image_src)) {
// Remove the base URL and '/wp-content/uploads/' path from the image URL
$url = str_replace($base_url . '/wp-content/uploads/', '', $image_src[0]);
// Add the image URL and dimensions to the "sizes" array
$data['sizes'][$size] = array(
'url' => $url,
'width' => $image_src[1],
'height' => $image_src[2],
);
}
}
}
// Return the image data array
return $data;
}
It works with:
add_filter('acf/rest/format_value_for_rest/type=image', 'jk_acf_restapi_filtering_image_format', 10, 3);
But i need use:
add_filter('acf/format_value/type=image', 'jk_acf_restapi_filtering_image_format', 10, 3);, the response of image is false.
I have custom endpoint, so i must use format_value.
Does format_value need any special return format?
You need to do either or both of the following
First if you have not set ACF to return the image ID for the image field then you need to change this. If you have it set to return an anything other than the ID then ACF may have already formatted the value, in this case $image = wp_get_attachment_image_src($value, 'full');
is likely generating an error. If you can’t change the return format then you will need to get the value without formatting instead of depending on the passed $value. $value = get_field($field['key'], $post_id, false);
Second you need to set the priority > 10. ACF has a built in filter for acf/format_value/type=image on priority 10. If this filter runs after your filter it will ignore any formatting you’ve done in your filter and do its normal formatting, whatever that is. If the filter runs before your filter and you have set ACF to return anything other and the ID it may have already done the formatting. See the first explanation. There is no guarantee which filter will run first when both have the same priority.
format_value does not need a special return format, ACF will return whatever you return from the filter as long as it runs after the ACF filter.
You must be logged in to reply to this topic.
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.