• Resolved bward

    (@bward)


    Hello!

    Each day I post news on our site, but do not always add a featured image to a post. Our posts are then automatically added to our Twitter/Facebook account where the same fallback image appears every time. Only when I add a featured image to the post does it change. No-fault of your software by the way.

    To social media users though, I am sure many scroll right past the posts with the same share image. Its human nature.

    How can we configure TSF to not display an image on posts unless the post has a featured image? Maybe just display the link with the favicon next to the link instead. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hi again!

    Thank you for opening a new topic. Your inquiry isn’t related to the one where you posted earlier since oEmbed is a different protocol from Open Graph & Twitter Card.

    The solution is simple: Only allow featured images to get through the generator via a filter.

    The execution for that is not that simple, but here it is:

    add_filter( 'the_seo_framework_image_generation_params', function( $params, $args, $context ) {
    
    	// Don't adjust non-social callers, such as Schema.org generators.
    	if ( 'social' !== $context ) return $params;
    
    	// When a featured image may be generated, make sure it's the only thing used for generation.
    	if ( isset( $args['cbs']['featured'] ) ) {
    		$args['cbs'] = array_intersect_key( $args['cbs'], array_flip( [ 'featured' ] ) );
    	}
    
    	// Disable fallback images.
    	$args['fallback'] = [];
    
    	return $args;
    }, 11, 3 );

    You can place that in your (child-)theme’s functions.php file or a custom (mu-)plugin.

    The filter modifies the image generator. What it does is disable fallback image lookups for all social context-queries (like Twitter Card and Open Graph). It also strips all other callbacks (cbs) but the featured-image-getter. You may want to forgo the cbs array alteration if you still want to allow images from the content to be processed.

    The social image you set via the SEO meta settings box always has precedence and is unaffected by this filter.

    I hope this helps ??

    Thread Starter bward

    (@bward)

    No problem Sybre, my error.

    I appreciate the code here. So this does do what you said. Twitter places the post into a small card and there is no image, just a default image icon twitter adds. That is not going to work out.

    I do appreciate you trying to help though!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Restrict Social Image Except For Featured Image’ is closed to new replies.