how to include custom image with share?

How would I include a custom thumbnail for the post/page being shared? Is there any way I can set it to pull the featured image as a thumbnail?

<a id="ref_fb" target="_blank" href="http://www.facebook.com/sharer.php?s=100&p[title]=<?php single_post_title(); ?>&p[summary]=<?php echo $description;?>&p[url]=http://domain.com/&p[images][0]=<?php echo $image; ?>"><img src="https://s3.amazonaws.com/bucket/images/social/social-fb_03.png" alt="fb share"/></a></div>

  • Ash

    Hello @Andrew

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    Would you please add this code:

    function insert_image_src_rel_in_head() {
    global $post;
    if ( !is_singular()) //if it is not a post or a page
    return;
    if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
    $default_image="http://example.com/image.jpg"; //replace this with a default image on your server or an image in your media library
    echo '<meta property="og:image" content="' . $default_image . '"/>';
    }
    else{
    $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
    echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
    }
    echo "
    ";
    }
    add_action( 'wp_head', 'insert_image_src_rel_in_head', 5 );

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Let me know if it works for you.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.