Filter uncanny-automator

automator_token_post_featured_image_size

Filters the featured image size used for posts imported by WP All Import.

add_filter( 'automator_token_post_featured_image_size', $callback, 10, 2 );

Description

Allows developers to filter the featured image size used by WP All Import when generating post content. Modify the image size before it's applied to the post, enabling custom display options for featured images inserted via the plugin.


Usage

add_filter( 'automator_token_post_featured_image_size', 'your_function_name', 10, 2 );

Parameters

$post (mixed)
This parameter represents the desired size for the featured image.
$to_replace (mixed)
This parameter is the post object for which the featured image size is being filtered.

Return Value

The filtered value.


Examples

/**
 * Change the featured image size used for the POSTIMAGEURL token.
 *
 * This filter allows you to dynamically control the image size that is
 * retrieved for the featured image when using the {POSTIMAGEURL} token.
 * For example, you might want to use a smaller thumbnail size for efficiency
 * or a specific custom image size registered in your theme.
 *
 * @param mixed $size The current image size being used (defaults to 'full').
 * @param int   $post_id The ID of the post for which the featured image is being retrieved.
 * @param mixed $to_replace The original token string that was replaced. (This parameter is often unused in simple examples).
 *
 * @return string The desired image size slug.
 */
add_filter( 'automator_token_post_featured_image_size', function( $size, $post_id, $to_replace ) {

	// Example: If the token being replaced is specific, like {POSTIMAGEURL_THUMBNAIL},
	// then we can force a 'thumbnail' size.
	// In a real-world scenario, you might check $to_replace for more complex logic.
	// For this example, we'll just demonstrate changing it to 'medium'.

	// Let's say we want to use the 'medium' size by default for this token.
	$new_size = 'medium';

	// You could also add conditional logic here. For instance, if the post
	// belongs to a specific category, you might use a different size.
	// $post_categories = get_the_category( $post_id );
	// if ( ! empty( $post_categories ) ) {
	// 	foreach ( $post_categories as $category ) {
	// 		if ( $category->slug === 'special-images' ) {
	// 			$new_size = 'large';
	// 			break;
	// 		}
	// 	}
	// }

	return $new_size;

}, 10, 3 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/integrations/wp-all-import/tokens/wpai-tokens.php:318
src/integrations/wp/tokens/wp-post-tokens.php:577
src/integrations/wp/tokens/wp-post-tokens.php:755

$content = str_replace( ']]>', ']]>', $content ); // phpcs:ignore Generic.PHP.Syntax.PHPSyntax
				$value   = $content;
				break;
			case 'POSTIMAGEID':
				$value = get_post_thumbnail_id( $post->ID );
				break;
			case 'POSTIMAGEURL':
				$value = get_the_post_thumbnail_url( $post->ID, apply_filters( 'automator_token_post_featured_image_size', 'full', $post->ID, $to_replace ) );
				break;
			case 'POSTAUTHORFN':
				$value = get_the_author_meta( 'user_firstname', $post->post_author );
				break;
			case 'POSTAUTHORLN':
				$value = get_the_author_meta( 'user_lastname', $post->post_author );
				break;


Scroll to Top