Hack: Shortcode To Include a Screen Grab of a Website

Wouldn’t it be nice if you were talking about a page at a website and you could easily include a screenshot of that page? It is actually very easy to do this once you add a shortcode to your functions.php or custom plugin. And the code is very short too.

It turns out that WordPress has a service that, given a URL, will return a screen grab of that site. It is so easy to call this service that it only takes a tiny little shortcode function to do so.

add_shortcode("mshot", "wpshottcode_mshot");
function wpshottcode_mshot($atts) {
    $a = shortcode_atts(array('url' => ''), $atts);
	return '<img src="http://s.wordpress.com/mshots/v1/' . urlencode($a['url']) . '?w=320&h=240">';
}

Now that you have this, you can use code like this to insert a screen grab:

[mshot url="https://wptipsandhacks.com"]

Note that the code sets the size of the image to be 320×240. You can easily change this, or make the width and height two more parameters of the shortcode.

Leave a Reply

Your email address will not be published. Required fields are marked *