public
Last active

Add screenshot of current page as Open Graph image. Primes the wp.com cache by including the image at the bottom of the page as well.

  • Download Gist
add_screenshot_as_ogimage.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?php
 
add_action( 'wp_head', 'add_screenshot_as_ogimage' );
 
function add_screenshot_as_ogimage(){
// default width = 1500 -- 200px minimum, Facebook recommends 1500x1500, max image size of 5mb
echo '<meta property="og:image" content="http://s.wordpress.com/mshots/v1/'.urlencode( $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ).'?w=1500"/>';
 
echo '<meta property="og:type" content="website">'."\n";
echo '<meta property="og:url" content="'. home_url() . $_SERVER["REQUEST_URI"].'"/>'."\n";
echo '<meta property="og:title" content="'.get_the_title().'"/>'."\n";
echo '<meta property="og:description" content="'.get_the_excerpt().'"/>'."\n";
 
}
 
// Optional
add_action( 'wp_footer', 'add_screenshot_as_ogimage_footer' );
 
function add_screenshot_as_ogimage_footer(){
//prime WP.com cache so FB gets the image and not a loading screen
echo '<img src="http://s.wordpress.com/mshots/v1/'.urlencode( $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ).'?w=1500" width="1" height="1" style="visibility: hidden;">';
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.