I've made use of the filters for social media images to add images to generated pages: "the_seo_framework_ogimage_output" and "the_seo_framework_twitterimage_output". These filters have been removed in version 4.
Do you see a chance of re-adding them? I'll gladly add a PR to get the filters back – I also have an idea of improving them.
Hello!
Because we now work with multi-item PHP 5.5 generators, we had to drop the old single-item filters. To accompany the new system, we have a new filter in place: the_seo_framework_image_generation_params:
https://github.com/sybrew/the-seo-framework/blob/f23bceb092cd183fde993e5429a1728a3f2bbba4/inc/classes/generate-image.class.php#L345-L371
Therein, you can append, prepend, and remove image callbacks. Any of the default callbacks will give you a hint on how to work with this. You can find them in this file.
Improvements to this are always welcome. But, if the new filter allows you to (re)integrate your system, then I think we already have a winner 😄
Hey, I'm having hard time replicating my filters using the new generator based filters. Here's an example of code that I used pre-4.0.0 to set a fallback image from custom field if social image was not selected:
add_filter('the_seo_framework_og_image_fallback', function ($image, $post_id) {
if ($image) {
return $image;
}
if (!the_seo_framework()->is_singular()) {
return $image;
}
$post = get_post($post_id);
if ($post && $image = get_field('npx_preview_image', $post)) {
if (!empty($image['sizes']['npx-1440'] && $image['sizes']['npx-1440-width'] === 1440)) {
return $image['sizes']['npx-1440'];
}
return $image['url'];
}
return $image;
}, 10, 2);
Any ideas how to replicate this with the new system?
Hi @joppuyo,
The new implementation should look a bit like the (untested) snippet below for you. I understand that it's a bit daunting at first. But, I think you'll realize it's infinitely expandable and amenable, all while supporting multiple image inputs.
add_filter( 'the_seo_framework_image_generation_params', 'my_tsf_custom_image_generation_args', 10, 2 );
/**
* Adjusts image generation parameters.
*
* @param array $params : [
* string size: The image size to use.
* boolean multi: Whether to allow multiple images to be returned.
* array cbs: The callbacks to parse. Ideally be generators, so we can halt remotely.
* array fallback: The callbacks to parse. Ideally be generators, so we can halt remotely.
* ];
* @param array|null $args The query arguments. Contains 'id' and 'taxonomy'.
* Is null when query is autodetermined.
* @return array $params
*/
function my_tsf_custom_image_generation_args( $params, $args ) {
if ( null === $args ) {
// In the loop.
if ( is_singular() ) {
// Append our npx callback.
$params['cbs']['npx'] = 'my_tsf_custom_singular_npx_generator';
}
} else {
// Out the loop. Use $args to evaluate the query...
if ( ! $args['taxonomy'] ) {
// Singular.
// Append our npx callback.
$params['cbs']['npx'] = 'my_tsf_custom_singular_npx_generator';
}
}
return $params;
}
/**
* Generates image URL and ID via npx.
*
* @generator
*
* @param array|null $args The query arguments. Accepts 'id' and 'taxonomy'.
* Leave null to autodetermine query.
* @param string $size The size of the image to get.
* @yield array : {
* string url: The image URL location,
* int id: The image ID,
* }
*/
function my_tsf_custom_singular_npx_generator( $args = null, $size = 'full' ) {
$post_id = isset( $args['id'] ) ? $args['id'] : the_seo_framework()->get_the_real_ID();
if ( function_exists( 'get_field' ) ) {
$image = get_field( 'npx_preview_image', get_post( $post_id ) );
yield [
'url' => ! empty( $image['url'] ) ? $image['url'] : '',
'id' => 0, // Does the image have a stored media ID? Fill it in, so it can obtain sizes.
];
} else {
// Since we don't support PHP 7.0+ only yet, we have to trigger the generator here on failure ("yield from" is unavailable).
yield [
'url' => '',
'id' => 0,
];
}
}
That worked perfectly, thanks @sybrew!
@joppuyo this is your ticket now :smile:
Most helpful comment
Hello!
Because we now work with multi-item PHP 5.5 generators, we had to drop the old single-item filters. To accompany the new system, we have a new filter in place:
the_seo_framework_image_generation_params:https://github.com/sybrew/the-seo-framework/blob/f23bceb092cd183fde993e5429a1728a3f2bbba4/inc/classes/generate-image.class.php#L345-L371
Therein, you can append, prepend, and remove image callbacks. Any of the default callbacks will give you a hint on how to work with this. You can find them in this file.
Improvements to this are always welcome. But, if the new filter allows you to (re)integrate your system, then I think we already have a winner 😄