Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Website Snapshots with WordPress Shortcode

We originally wrote this code in 2010 with the expectation that WordPress would cease proving their screenshot API to the public. In the typical open spirit of WordPress, however, WP has stated that usage is fine as long as there's no expectation it'll last forever (we're sure that WP never intended for their service to be used indiscriminately by the online world). The feature was originally used to display screenshots of Commercial Themes . It's since made its way into your administration dashboard to associate URLs with website snapshots.

The following functions make use of the WordPress snapshot API and renders a screenshot on any post or page.

The Result

The shortcode of [snap url="http://www.flight.org/" alt="Flight" width="530" height="280"] will return the following image:

Flight

If the image isn't immediately available you'll see a preview image. The API appears to return an image with a maximum resolution of 1280 x 960 pixels.

The Code

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

1
<?php 
2
/*
3
    Website Snapshots with WordPress Shortcode
4
    http://www.beliefmedia.com/website-snapshots
5
*/
6
 
7
 
8
function beliefmedia_wp_snapshot($atts, $content = null) {
9
 
10
  $atts = shortcode_atts(array(
11
   "snap" => 'http://s.wordpress.com/mshots/v1',
12
   "url" => 'http://www.flight.org/',
13
   "alt" => false,
14
   "width" => '400',
15
   "height" => '300',
16
   "align" => 'center',
17
  ), $atts);
18
 
19
  /* If no alt text, we'll display the URL as alt text */
20
  if ($atts['alt'] === false) $atts['alt'] = $atts['url'];
21
 
22
 return '<p><div style="text-align:' . $atts['align'] . '"><img alt="' . $atts['alt'] . '" src="' . $atts['snap'] . '/' . urlencode($atts['url']) . '?w=' . $atts['width'] . '&h=' . $atts['height'] . '"></div></p>';
23
}
24
add_shortcode("snap", "beliefmedia_wp_snapshot");

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

You can define the website URL (url), alt text (alt), width (width), height (height), and image alignment (align) by way of shortcode attributes.

The first time you call the website it will take some time to render (it seems to cache on WordPress for an unknown period after that). I suggest you preview the post first and let the screenshot render before you publish your post to the world.

PHP Function

Using the function outside of WordPress seems a little unethical and I wouldn't recommend it. However, for the sake of completeness, the following PHP function will return a snapshot.

1
<?php 
2
/*
3
    Website Snapshots with WordPress Shortcode
4
    http://www.beliefmedia.com/website-snapshots
5
*/
6
 
7
 
8
function beliefmedia_wp_snapshot($url, $args = '') {
9
 
10
  $atts = array(
11
   "snap" => 'http://s.wordpress.com/mshots/v1',
12
   "url" => 'http://www.flight.org/',
13
   "alt" => false,
14
   "width" => '400',
15
   "height" => '300',
16
   "align" => 'center',
17
  );
18
 
19
  /* If no alt text, we'll display the URL as alt text */
20
  if ($atts['alt'] === false) $atts['alt'] = $atts['url'];
21
 
22
  /* Merge $args with $atts */
23
  $atts = (empty($args)) ? $atts : array_merge($atts, $args);
24
 
25
 return '<p><div style="text-align:' . $atts['align'] . '"><img alt="' . $atts['alt'] . '" src="' . $atts['snap'] . '/' . urlencode($atts['url']) . '?w=' . $atts['width'] . '&h=' . $atts['height'] . '"></div></p>';
26
}
27
 
28
 
29
/* Usage */
30
$url = 'http://www.flight.org/';
31
32
 
33
 
34
/* With arguments */
35
$args = array('width' => 800, 'height' => 600);
36
echo beliefmedia_wp_snapshot($url, $args);

Considerations

  • Given the nature of the source, respect the limits of your use. Don't expect any longevity from the service.
  • We have our own screenshot tool at Screenshotz . We'll soon be migrating all the features of that site into BeliefMedia and retiring the old platform.

Download

Title: Website Snapshots with WordPress Shortcode (WP Plugin)
Description: Shortcode makes use of the WordPress snapshot API and renders a website screenshot of any page or post.
  Download • Version 0.2, 1.4K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (589.0B)    PHP Code & Snippets, (658.0B)    

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment

READY TO HAVE A CHAT? CALL US ANYTIME ON 1300 235 433