Skip Navigation

[Resolved] Resize slug image in API Custom Type

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by avansisI-2 4 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1880129

Good morning

I am trying to implement a code to be able to send to my application in the rest API of my custom type the slug of the image with the size 300x that wordpress generates. How can I do it?

#1880165

In woocommerce i have this code, but i dont know customize to custom fields in toolset


function prepare_product_images($response, $post, $request) {
    global $_wp_additional_image_sizes;

    if (empty($response->data)) {
        return $response;
    }

    foreach ($response->data['images'] as $key => $image) {
        $image_urls = [];
        foreach ($_wp_additional_image_sizes as $size => $value) {
            $image_info = wp_get_attachment_image_src($image['id'], $size);
            $response->data['images'][$key][$size] = $image_info[0];
        }
    }
    return $response;

}

add_filter("woocommerce_rest_prepare_product_object", "prepare_product_images", 10, 3);

#1880197
function add_image_attrs() {
	register_rest_field('post', 'image-dimensions', array(
		'get_callback' => 'getImageProperties',
		'update_callback' => null,
		'schema' => null,
	));
}

add_action('rest_api_init', 'add_image_attrs');

function getImageProperties() {
	// current Post
	global $post;
	// regex to get all image ID's within Gutenberg post content, initialize $matches variable, set results to that
	$imageIds = array();
	if(preg_match_all('<!-- wp:image {"id":(\d+)} -->', $post->post_content, $matches)) {
		array_push($imageIds, $matches[1]);
	}; 

	if(preg_match_all('#wp-image-(\d+)#', $post->post_content, $matches)) {
		array_push($imageIds, $matches[1]);
	};

	if (empty($matches[1])) {
		return;
	}
	
	// map over $matches array, get metadata using each ID, return ID, width, height
	$results = array_map(function($imageId) {
		$image = wp_get_attachment_metadata($imageId);
		return [
			'id'=>(int)$imageId,
			'width'=>$image['width'],
			'height'=>$image['height']
		];
	}, $matches[1]);
	return $results;
}
#1880349

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Toolset offers the REST integration and here is the Doc for that:
=> https://toolset.com/documentation/programmer-reference/toolset-integration-with-the-rest-api/#how-the-integration-works

For the data return in REST, for the image fields it returns the attachent ID as you can see with the following table:
=> https://toolset.com/documentation/programmer-reference/toolset-integration-with-the-rest-api/#field-types-and-their-value-format-in-rest-api

I would like to know on what endpoint you would like to generate the image with your custom size 300x300 - correct? and you want to return the 300x300 image URL as a result of your endpoint- right?

#1883079

My issue is resolved now. Thank you!