sybrew/the-seo-framework

Change og:image parameters

Closed this issue · 2 comments

I have difficulty finding the filter to change the OG parameters: og:image, og:width and og:height.
I use a real-time image converter and I need to change these properties to add for example: compress.com/widthxheight/ORIGINAL-IMAGE-URL

Hello!

It's tricky because we had to implement the straightforward WordPress Filter API into the next-gen PHP Generator API.

To add, replace, or remove image generators, please see https://tsf.fyi/kb/filter-reference-for-the-seo-framework/#image-related.

To modify the current image details array, use the_seo_framework_image_details.

/**
* @since 4.0.5
* @since 4.2.0 Now supports the `$args['pta']` index.
* @param array $details The image details array, sequential: int => {
* string url: The image URL,
* int id: The image ID,
* int width: The image width in pixels,
* int height: The image height in pixels,
* string alt: The image alt tag,
* }
* @param array|null $args The query arguments. Accepts 'id', 'taxonomy', and 'pta'.
* Is null when query is autodetermined.
* @param bool $single Whether to fetch one image, or multiple.
* @param string $context The filter context. Default 'social'.
* @param bool $clean Whether to clean the image, like stripping duplicates and erroneous items.
*/
return \apply_filters_ref_array(
'the_seo_framework_image_details',
[
$clean ? $this->s_image_details( $details ) : $details,
$args,
$single,
$context,
$clean,
]
);

I recommend dumping the first parameter ($details) to learn how the data is composed.
The results of this data may not be reflected in the administrative input fields; however, they will affect the front end.
Perhaps I should add more filters to allow for easier and more robust integrations.

That was enough! Thank you very much! 👍

add_filter('the_seo_framework_image_details', function ($details) {
	$thumbor = new Thumbor(THUMBOR_HOST, THUMBOR_KEY);
	$width = 1200;
	$height = 600;

	foreach($details as &$detail) {
		$raw_image = image_rewrite($detail['url']);
		$detail['url'] = $thumbor->resize($width, $height)->smartCropEnable()->imageUrl($raw_image)->get();
		$detail['width'] = $width;
		$detail['height'] = $height;
	}

	return $details;
});