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

Add default image to custom post type to be used as images in news article schema #78

Open
bonakor opened this issue Aug 10, 2023 · 2 comments

Comments

@bonakor
Copy link

bonakor commented Aug 10, 2023

Hello,
On some of my custom posts, I have no featured image nor images in the content. I'd like to have a default image added automatically to the schema news article, how can I do it, please?

Is it the same process than modifying the output of default description or title?

@sybrew
Copy link
Owner

sybrew commented Aug 14, 2023

Hello!

Changing how the image generator works is different from the title and description generators. This is because the description is a string (there may only be one), whereas images are an array of strings (Open Graph and Schema.org allow multiple images). So, instead of assigning a value directly, we assign a generator (which yields a value).

Depending on the queue position of your generator (assigned before or after), it may influence whether the image is used under certain conditions. We recommend users to only set one image for Open Graph, so only the first good image will be used, and the other images won't even be looked up. This means that only the first few generators could be called, and all following won't be called. This is the beauty of generators; code can be halted remotely -- almost naturally.

Generators must always yield a value and may optionally return a value. Hence, the syntax is a little different.

The snippet below may do. Two things before it works:

  1. Set $post_type's value to your custom post type.
  2. Set 'url' to your image URL in my_custom_tsf_image_generator(). Also set 'id' to the attachment's ID so dimensions can be obtained.
add_filter( 'the_seo_framework_image_generation_params', 'my_tsf_custom_image_generation_args', 10, 3 );
/**
 * Adjusts image generation parameters.
 *
 * @link https://theseoframework.com/docs/api/filters/#append-image-generators-for-social-images
 * @param array      $params  : [
 *    string  size:     The image size to use.
 *    boolean multi:    Whether to allow multiple images to be returned. This may be overwritten by generators to 'false'.
 *    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', 'taxonomy', and 'pta'.
 *                            Is null when the query is auto-determined.
 * @param string     $context The filter context. Default 'social'.
 *                            May be (for example) 'breadcrumb' or 'article' for structured data.
 * @return array $params
 */
function my_tsf_custom_image_generation_args( $params, $args = null, $context = 'social' ) {

	switch ( $context ) {
		case 'sitemap':
		case 'schema':
			// Filter only sitemap and schema.
			break;
		default:
			// Do not filter the rest
			return $params;
	}

	// Set this to your post type name:
	$post_type = 'my_post_type';

	if ( null === $args ) {
		$is_my_post_type = is_singular( $post_type );
	} else {
		// Out the loop. Use $args to evaluate the query...
		if ( ! $args['taxonomy'] && ! $args['pta'] ) {
			$is_my_post_type = get_post_type( $args['id'] ) === $post_type;
		}
	}

	if ( ! empty( $is_my_post_type ) ) {
		$params['cbs'] = array_merge(
			[ 'custom' => 'my_custom_tsf_image_generator' ], // prepend to regular callbacks.
			$params['cbs'],
		);
	}

	return $params;
}

/**
 * Generates image URL and ID via my_get_image_value.
 *
 * @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_custom_tsf_image_generator( $args = null, $size = 'full' ) {
	// Yield the image...
	yield [
		'url' => 'https://example.com/path/to/image.jpg',
		'id'  => 0, // Optional image ID. But required to fetch image dimensions.
	];
}

@bonakor
Copy link
Author

bonakor commented Sep 1, 2023

Hello @sybrew,
Thank you for this detailed answer (as always).

Sorry for the delay in mine, I was enjoying a summer break.

I'll implement this next week and get back to you if needed.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants