easier way to swap out thumbnail info? I hope so (2 posts)

  1. vincej
    Posted 5 years ago #

    Hope this is the right forum, if not let me know..

    objective: to get a thumbnail to use for facebook meta tags.
    specifically, I am trying to make this tag dynamic.
    <meta property="og:image" content="http://greenspotantiques.com/images/my_fb_logo.jpg"/>

    I wish to retrieve a thumbnail URL to hand over to Facebook for each post page.
    The problem: facebook wants images in a 50 x 50 format.
    I'm in luck, I had already defined WP to generate 50x50 thumbs (by default I think?) -- so all I need to do is figure out a way to "get" the url to that image.

    the 50x50 image is NOT used in my theme, but it exists.
    So I did the following..... but there's gotta be a simpler and shorter method.

    Logic: if it's a single post page, I want to check if there IS a thumbnail, if there is, I need to read in it's URL, and parse it out replacing the XX x XX tag with 50x50 and feed it out as a string, only IF the file exists. (in some cases it may not) - in that case, I'd feed out the my_fb_logo.jpg standard website logo...
    (I've included the img src portion to test result, would be excluded in final).

    (reason for help? simple, this looks like a pretty hefty call as it would occur every time a page is loaded, in the header.... )
    The original url being parsed out is in the form of

    if(is_singular()) { //only for single posts or pages
    $myID = get_post_meta(get_the_ID(), '_thumbnail_id', true); // returns IDentity number of thumb
    $arr =wp_get_attachment_image_src($myID ); // to get the actual url of thumb
    if(!empty($arr[0])) { // if empty bail
    $str = $arr[0]; // first element is the URL, then sizes, etc
    $myurl= (explode("/",$str)); // splits url into parts
    $mybasehref= 'http://myserver.com'   .  $myurl[0] . '/' .  $myurl[1] . '/' . $myurl[2] . '/' .  $myurl[3] . '/' . $myurl[4] . '/' .  $myurl[5] . '/' ;  // results in URL without filename
    $myimg=  (explode("-",$myurl[6]));  // extracts the filename
    if(file($mybasehref . $myimg[0] . '-' . $myimg[1] . '-'  . $myimg[2] .'-50x50.jpg')) {
    echo ('<img src="'. $mybasehref . $myimg[0] . '-' . $myimg[1] . '-'  . $myimg[2] .'-50x50.jpg' . '">' ); // test with img, to delete in final
    echo ($mybasehref . $myimg[0] . '-' . $myimg[1] . '-'  . $myimg[2] .'-50x50.jpg'); // this would replace the "content" in meta tag for facebook.
     } }

    the result of the above is
    a working URL.

  2. vincej
    Posted 5 years ago #

    For anyone with interest the result thus far is good, but it would be nice to have cleaner, more efficient code to parse the text.

    The result of the above is seen here

    It swaps out the post thumbnail for the smaller size, and feeds it out to Facebook's social graph for search optimization thru their custom meta properties.

    I placed the code above into my functions.php file, function called as below.

    <meta property="og:image" content="<?php swapthumbURL(); ?>"/>

    as mentioned, the result is a per above LINT check on Facebook. Each post or page now, with thumbnail is fed out either a default image (has no thumbnail), or the post-specific thumbnail in 50x size.

    I'm pleased with the result but not the code, looks clumsy.
    php wizard requried, i'm just a green apprentice here :-)

Topic Closed

This topic has been closed to new replies.

About this Topic