• Resolved Grzegorz.Janoszka

    (@grzegorzjanoszka)


    Hi,
    I have been a happy use of this plugin and I have used the filter called the_seo_framework_og_image_after_featured. Recently I noticed this filter is never called anymore and TSF finds indeed always a correct image but not in the size I want it to (it finds the small one which is in the post and not the big one linked).

    I have checked https://theseoframework.com/docs/api/filters/ but there are only two functions related to images and I don’t see how I could use them to use my image and not the one selected by TSF.
    How can I interfere with the social image used by this great plugin?

    Thank you in advance.

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

    (@cybr)

    Hi Grzegorz,

    That’s correct! Since TSF 4.0, we use generators for images. Via the mere two filters we provide, you can make the generator do anything your heart desires, but it requires a bit of learning–once you understand how it works, a whole new world of programming with PHP opens ??

    Are you referring to this snippet, here? https://www.ads-software.com/support/topic/wrong-image-used-and-two-other-remarks/#post-7157603

    I think you can use the filter described here as-is. But, you’ll need to unset $params['cbs']['content'], and append your custom callback to $params['cbs'] (the example already does the latter). The custom callback needs to yield the URL and ID of the image of your choosing.

    In short, you’ll need to change:

    $params['cbs']['custom'] = 'my_tsf_custom_singular_image_generator';
    

    To:

    unset( $params['cbs']['content'] );
    $params['cbs']['custom'] = 'my_tsf_custom_singular_image_generator';
    

    Aside: If you wish to retain the TSF-implemented content callback, but wish to put in your custom callback right before that, please refer to this StackOverflow answer. It’s a bit cumbersome in PHP… we may want to implement “priorities” to work around this easily.

    The callback function will look a bit like this (based on the comment you made earlier):

    function my_tsf_custom_singular_image_generator( $args = null, $size = 'full' ) {
    
    	$post_id = isset( $args['id'] ) ? $args['id'] : the_seo_framework()->get_the_real_ID();
    
    	$args = [
    		'numberposts'    => 1,
    		'order'          => 'ASC',
    		'post_mime_type' => 'image',
    		'post_parent'    => $post_id,
    		'post_type'      => 'attachment',
    		'no_found_rows'  => true,
    	];
    
    	$img = array_shift( get_posts( $args ) )->guid ?? '';
    
    	if ( ! $img ) {
    		$img = home_url( '/default-image.jpg' );
    	}
    
    	yield [
    		'url' => $img,
    		'id'  => 0,
    	];
    }

    This is the amalgamated snippet: https://gist.github.com/sybrew/2f4e1fc1b5814fe813328009520a322c

    Thread Starter Grzegorz.Janoszka

    (@grzegorzjanoszka)

    Sybre,

    As usually fantastic answer. Sorry, I was not aware of generators and yields, but I have learned something new today – thank you!

    I have my code that works. One question though – currently when I am returning several images I yield them with ID’s 0, 1, 2… and so on.
    Is that correct or should they all have ID 0?

    I see multiple og:image tags in my html code, but somehow Facebook uses only first of them – are you aware of how FB treats multiple og:images? I think you may have more experience with how FB treats the tags generated by TSF.

    Thank you one more time for fantastic support!

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Grzegorz,

    When you supply ID 0, TSF will not parse the image–it’ll accept the image URL as-is and output it. If you do supply an ID, TSF will then try to parse the image, fetch the correct resolutions for it, and filter images that are either too large or too small. The resolutions found are outputted as extra metadata, which can help speed up Facebook’s caching process.

    The Open Graph standard supports multiple images. See https://ogp.me#array. Facebook can sometimes output an interactable ‘gallery’ when they encounter an array of images; however, I couldn’t find any concise documentation on this from them.

    Thread Starter Grzegorz.Janoszka

    (@grzegorzjanoszka)

    Sybre, I still don’t know what is the difference between ID=1 and ID=2, but anyway – going with your advise I return only ID=0 now ??
    Thank you one more time for fantastic support!

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Grzegorz,

    Sorry about that! The ID corresponds to the attachment ID which WordPress generates for each uploaded attachment. This attachment has metadata, that holds cropped image sizes, the image locations, attached posts, uploader info, etc.

    Here’s one function that the ID can be used for, which we use to obtain the largest acceptable image available:
    https://developer.www.ads-software.com/reference/functions/wp_get_attachment_metadata/

    If you know the attachment ID from the image (without performing extraneous expensive database lookups!), then feel free to yield it for added sanitization in TSF.

    • This reply was modified 4 years, 1 month ago by Sybre Waaijer. Reason: More details
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Filter for images’ is closed to new replies.