Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Filters for og:image and twitter:image #470

Closed
alpipego opened this issue Sep 17, 2019 · 4 comments

Comments

@alpipego
Copy link

commented Sep 17, 2019

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.

@sybrew

This comment has been minimized.

Copy link
Owner

commented Sep 18, 2019

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:

/**
* @since 4.0.0
* @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.
* @param string $context The filter context. Default 'social'.
* May be (for example) 'breadcrumb' or 'article' for structured data.
*/
return \apply_filters_ref_array(
'the_seo_framework_image_generation_params',
[
[
'size' => 'full',
'multi' => true,
'cbs' => $cbs,
'fallback' => $fallback,
],
$args,
$context,
]
);
}

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 😄

@joppuyo

This comment has been minimized.

Copy link

commented Sep 20, 2019

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?

@sybrew

This comment has been minimized.

Copy link
Owner

commented Sep 23, 2019

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,
		];
	}
}
@alpipego

This comment has been minimized.

Copy link
Author

commented Sep 25, 2019

That worked perfectly, thanks @sybrew!
@joppuyo this is your ticket now 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.