How to Generate Website Screenshots For Your WordPress Site

How to Generate Website Screenshots For Your WordPress Site

Tutorial Details
  • Program: WordPress
  • Version: 3.0+
  • Difficulty: Beginner
  • Estimated Completion Time: 30 minutes

In this tutorial you’ll learn an open secret: how to generate screenshots of websites to use in your WordPress posts using a service provided by WordPress.com. We’ll even turn this into an easy-to-use shortcode to display the screenshots… let’s get started!


Background

It’s an open secret, and has been for a couple of years, that WordPress exposes a screenshot generator at it’s s.wordpress.com subdomain. And the scuttlebutt is that as long as you don’t abuse it, then it’s free to use. WordPress.com uses it for generating screenshots for it’s various activities.

For instance, here’s the url to show a screenshot of wp.tutsplus with a width of 400 pixels:

http://s.wordpress.com/mshots/v1/http%3A%2F%2Fwp.tutsplus.com?w=400

You may need to click the link again as the generator takes a little time to cache the image.

What we’ll be doing:

  • write a shortcode function to display a screenshot
  • use the shortcode to display a screenshot as part of the post loop using a custom field for the url

Step 1 The Shortcode Function

Our shortcode will be used like:

[ss_screenshot width='300' site='http://wp.tutsplus.com']

You can add the code to the functions.php file in your theme directory. The full code is available in the source link above.

add_shortcode('ss_screenshot', 'ss_screenshot_shortcode');

function ss_screenshot_shortcode($atts){

  $width = intval($atts['width']);
  
  $width = (100 <= $width && $width <= 300) ? $width : 200;
  
  $site = trim($atts['site']);
  
  if ($site != ''){
  
    $query_url =  'http://s.wordpress.com/mshots/v1/' . urlencode($site) . '?w=' . $width;
    
    $image_tag = '<img class="ss_screenshot_img" alt="' . $site . '" width="' . $width . '" src="' . $query_url . '" />';
    
    echo '<a class="ss_screenshot_link" href = "' . $site . '">' . $image_tag . '</a>'; 
  
  }else{
  
    echo 'Bad screenshot url!';
  
  }
}

What this code does:

  • declare a shortcode called ss_screenshot with the add_shortcode function
  • the shortcode function accepts the $atts array containing the two shortcode attributes: width and site
  • the width attribute is converted to an integer and checked to make sure it’s within a suitable range, in this case between 100 and 300 pixels. Note: we use the PHP ternary operator to assign a default width of 200 if the supplied width is outside our range. This also takes into account a missing width attribute.
  • the site attribute is trimmed to remove accidental left and right spaces
  • we create a query url by adding a urlencoded site attribute and the width attribute
  • finally we wrap the query_url in an image tag and a link tag so that users can click the screenshot and be taken to the site

Step 2 Test The Shortcode

In a post or page, insert this shortcode:

[ss_screenshot width='300' site='http://amazon.com']

When you view the post or page, you should see a 300 pixel wide screenshot of Amazon. Remember, you may have to refresh the page after a few seconds to see the cached screenshot. Play around with the width and watch what happens when you go out of range. By the way, you can set any width range you like. Also, test a bad url: you should see the warning message instead of a screenshot.

Now that we have a working shortcode, let’s see how we can incorporate that into our WordPress loop.


Step 3 Custom Field Set-Up

Let’s imagine a scenario where we want to display a screenshot of a site beside each of our posts. For formatting and usability reasons, it’s best to automate this process by adding a custom field to our post edit screen. Into that field we can input a site url which can then be used in the post loop to display a screenshot using our new shortcode.

If the custom field box is not showing on your post edit screen, then look at screen top right and you should see the Screen Options tab. Click that and tick the Custom Fields box. Now you should see something like this at screen bottom:

Click the Enter New link and create a new custom field called screenshot_url. This is what you should see:

Now when you create/edit a post (the custom field appears for all posts) you can input a url for generating a screenshot related to the post.


Step 4 Add Screenshot Display To The Loop

Here’s a seriously cut-down version of the standard WordPress post loop (as found in a theme template file) including our screenshot display code:

if (have_posts()) while (have_posts()) : the_post();

  the_title();
  
  $url = get_post_meta($post->ID, 'screenshot_url', true);
  
  do_shortcode('[ss_screenshot width="280" site="' . $url . '"]'); 
  
  the_content();

endif;
endwhile;

What this code does:

  • check if we have posts, start looping and display the post title
  • get the contents of the screenshot_url custom field by passing the current post ID and the name of the custom field to the get_post_meta function
  • because we are in a template file, we use the do_shortcode function to parse our shortcode which includes the screenshot url for this post
  • then we display the post content and end the loop

Step 5 Test The Loop

If you created a post with the title Honda Accord and gave it a screenshot url of http://www.honda.com, using your new loop setup, you should see something like this:


Conclusion

Instead of displaying a harsh warning message if a post doesn’t have a screenshot url attached, you could show a default image.

If you don’t wish to use the screenshot generator from WordPress, or if you have larger needs, you could use the free option at a screenshot service like http://www.shrinktheweb.com. You could apply the techniques used in this tutorial to any screenshot service.

Things to remember:

  • the screenshots take a few seconds (usually) to generate, so refresh the page
  • urlencode the url sent to the screenshot service
  • begin your urls with http://
  • if you have more demanding needs for screenshots, use a commercial service
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Pingback: My Stream » How to Generate Website Screenshots For Your WordPress Site

  • Pingback: How To Build A Wordpress Website | Internet Marketing with Wordpress

  • http://99likes.com/ Likes

    Oh Great Man ! Thakyou for posting this Stuff , we are looking or searching for this kind of post from few days

  • http://mes-live.com Brenda Campbell

    Awesome post! Very nicely explained. I do trainings for WordPress and many people will fine this very helpful.

    Love your site too!

    Brenda Campbell

  • http://www.rockitweb.co.uk Rockit Web

    Wow now that is a great shortcode, thanks it will definitely come in useful :)

  • http://www.ariona.net Rian Ariona

    FYI, WordPress use MShot plugin to generate the screenshot from given url, it’s available on wordpress plugins repositories :)

  • http://www.famousbloggers.net/ Hesham

    Excellent, I have been looking for something like this for a long time, will help a lot in a few projects I am working on!

    Thanks guys, this is just awesome!

  • Pingback: Best WordPress Resources from Around the Web | WP Kube

  • Simon Johnstone

    Excellent! this will be great for my portfolio.
    Keep the good work

  • http://www.fakeword.com/baby/ Baby Cakes

    I didnt realize WordPress and PHP could do these sort of things. Cool tutorial, a bit over my head in parts but I’m new to all this stuff, and a bit of a dummy.

  • Pingback: WordPress Resource Roundup for the Week Ending November 19th - Charleston WordPress User Group

  • Pingback: Adding Screen Shots of Websites in WordPress Blog Posts | WPin.me

  • http://www.sanjaykhemlani.co, sanjay

    Nice! I didn’t know about that, thanks for sharing this tut!

  • http://www.raycreationsindia.com Ray Creations – Web Design

    We work a lot with WordPress and this definitely valuable information. By the way, is this service meant for public use?

  • http://peterolle.com Peter

    Is it possible to use that screenshot as a “featured image” for the posts ?

  • http://dribbble.codedevelopr.com/ jasondavis

    I highly recommend making your code grab the image and cache it locally

  • http://www.agnelwaghela.bammz.net/ Agnel Waghela

    hii Ross, I like this post.. its very nice…
    but already there exist a wordpress plugin for generating website screenshot…
    you can find it here http://goo.gl/5qHvi

    • Gold

      thanks Agnel Waghela, I’ll check WordPress Snap plugin that you recommended, hope it’s what I need. If not – I will follow this tutorial.
      Thanks Ross, great to see ‘difficulty level’ and ‘completion time’ on your tutorials, good to know before the start.

  • Pingback: Create Wordpress Shortcode to display Websites Screenshot Thumbnails | Zorem

  • http://appspyre.com Omari Celestine

    Is there anyway to do this with php for non-wordpress sites?

  • Pingback: How To Capture Or Generate Dynamic Screenshot On Any Webpage | Saket Jajodia

  • Pingback: Informacja test v2 | Vendeta.pl

  • Pingback: Generate Screenshots For Your WordPress Site | Wptuts+ | Jamochai

  • Mark Kal

    Thanks man! You just save lot of my time.

    Really appreciate the help. Thanks for the code!

  • Albert

    Nice tip!!

    Thanks!!!!!

  • http://www.kevinleary.net Kevin Leary

    This, along with some helpful advice from BinaryMoon, has been used for the creation of the Browser Shots plugin. I’ve just released a stable plugin for handling this, it makes the process of automatically generating website screenshots in WordPress dead simple with a simple shortcode including a TinyMCE icon & dialogs for the less technical user.

  • yumna

    thnx for this its really helpful, but what if i want to take the screenshot of the whole page i.e, is there any way to specify the length also??? reply soon plz,

  • http://www.facebook.com/mittul.chauhan Mittul Chauhan

    this is really a nice tutorial in deed .. .

  • Pingback: Web page preview with jQuery and Wordpress Mshots | Erik van de Ven