WordPress: Display screenshot of any website using shortcode

WordPress: Display screenshot of any website using shortcode

Do you often use screenshots of websites in your WordPress blog? You can make your blogging faster using a shortcode to add screenshot automatically. Copy-paste the following code snippet to the functions.php file of your WordPress theme. It will let you use the below mentioned shortcode to display a screenshot of any website. The default width and height of the screenshots are defined in pixels. You can change them to meet your requirements.

function wps_screenshot($atts, $content = null) {
        extract(shortcode_atts(array(
			"screenshot" => 'http://s.wordpress.com/mshots/v1/',
			"url" => 'http://',
			"alt" => 'Screenshot',
			"width" => '400',
			"height" => '300'
        ), $atts));
		return $screen = '<img src="' . $screenshot . '' . urlencode($url) . '?w=' . $width . '&h=' . $height . '" alt="' . $alt . '"/>';
}
add_shortcode("screenshot", "wps_screenshot");

Now to use this shortcode to add a thumbnail or screenshot of a website, you can use the following code with four attributes. You must add the URL parameter. If the rest are absent, defaults will be used. You can place this shortcode in your posts and pages.

[screenshot url="https://smartwebworker.com/"]
[screenshot url="https://smartwebworker.com/" alt="Free Web Design Resources"]
[screenshot url="https://smartwebworker.com/" alt="Free Web Design Resources" width="200" height="200"]