• How To?
  • Tips ‘n Tricks
  • WordPress
  • Snippets
  • Software
    • Browsers
    • Downloads
  • Web
  • Tools
    • Character Counter
    • chmod Calculator
    • Entities Encoder
    • Live HTML Editor
    • My IP
  • Contact
Twitter Facebook Google+ RSS
You are here: SumTips » How To? » Implement jQuery Lazy Load Images in WordPress Theme Manually

Implement jQuery Lazy Load Images in WordPress Theme Manually

Posted on July 6, 2012 by Renji | Short URL: http://sumtips.com/?p=7141

You might have seen images loading with a “fade-in” effect as you scroll down on some websites. That effect is called “Lazy Load”.

If you run an image based blog, or post several images in a single post, it can take a while for your site to load completely. And as you know, visitors and search engines alike do not appreciate slow websites. A solution to this issue is to load images only on demand. We can do this using a jQuery plugin called Lazy Load. It’s just 3 KB (minified) in size, and works nicely in all popular browsers.

Here are the steps to implement lazy loading images feature in a WordPress theme:

Step 1

Download the plugin from GitHub. jquery.lazyload.js is the original file, and jquery.lazyload.min.js is the minified version. Grab the desired version. There are several examples available for download as well there.

Upload the file to your theme’s script folder.

Step 2

Drop this code into your active theme’s functions.php file with the correct path to image:

function sumtips_lazy_load() {
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="http://yoursite.com/wp-content/themes/theme/scripts/jquery.lazyload.mini.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
jQuery(document).ready(function($){
	$("img.lazy").lazyload({
	effect : "fadeIn"
	});
});
</script>
<?php }
add_action('wp_head','sumtips_lazy_load');

Here JavaScript is added to head section of the page. If you’d rather like it in the footer, replace add_action('wp_head','sumtips_lazy_load'); with add_action('wp_head','sumtips_lazy_load');.

Also, if you’re already using jQuery, remove the reference to that from the code.

Step 3

Now when you add an image in a post, put the placeholder image into src attribute, and URL of the real image in an attribute called data-original. Like this:

<img class="lazy" src="lazyload.gif" data-original="postimage.jpg" />

In earlier versions of Lazy Load there was no need of this step as the JavaScript used to do everything automatically for us, but since newer browsers load images even without the src attribute, this step is necessary.

If you want, you can rename data-original parameter to something else. Here’s how you can do that:

$("img.lazy").lazyload({
  data_attribute  : "sumtips"
});

Now you can use it like this:

<img class="lazy" src="lazyload.gif" data-sumtips="postimage.jpg" />

Step 4

Most users have JavaScript enabled in their browser, but for those who don’t have, we can degrade lazy load functionality gracefully like this:

<img class="lazy" src="lazyload.gif" data-original="postimage.jpg" />
<noscript><img src="postimage.jpg" </noscript>

Just include the real image inside <noscript> block.

To prevent both placeholder and the real image showing up at the same time, hide the placeholder by default with css:

.lazy {
  display: none;
}

If the browser has JavaScript enabled, we can automatically display placeholder with this code:

$("img.lazy").show().lazyload();

That’s all!


Tweet

Related posts:

  • Implement Infinite Scrolling on a WordPress Theme
  • Create a jQuery Ticker Showing Latest Feed Items on WordPress
  • Implement Google Prettify Code Highlighting in WordPress the Manual Way
  • Give Your Site Visitors a Quick Print Preview Option, Ctrl+P Hotkey
  • Rename Original Filenames of Uploaded Images and Videos on WordPress
Categories: How To? | Tags: jQuery, WordPress
Check Whether ISO Image is Bootable Without Burning to Disc
Switch Between Multiple Power Plans with Shortcut or Schedule

  • Get Updates via Email

  • Recent Posts

    • CMS2CMS: Migrate Site from Drupal or Joomla to WordPress
    • WordPress: Add Preview Button in Distraction Free Writing
    • How to Activate or Deactivate Individual Jetpack Modules
    • Windows 8: Auto Update Defender with Windows Update Disabled
    • Automatically Start and Close Programs at Specific Time
    • How to Copy Code from Notepad++ with Syntax Highlighting
  • Random Posts

    • Google Maps 4.6 for Android
    • Add Create Folder List, Copy File/Folder Name to Explore Context Menu
    • Illuum – Monitor and Analyse Your Mood Online
    • How Long You Should Steep your Tea ?
    • Get Color-Coded Post Statuses in WordPress with UI Labs
    • Defragment Specific File or Folder in Windows
  • Categories

    • Blogging
    • Games
    • Google
    • How To?
    • Linux
    • Microsoft
      • Windows
    • Miscellaneous
    • Phone
    • Snippets
      • AutoHotkey Snippets
      • CSS Snippets
      • htaccess Snippets
      • JavaScript Snippets
      • PowerShell Snippets
      • WordPress Snippets
    • Social
    • Software
      • Browsers
    • Tips 'n Tricks
    • Wallpapers
    • Web
© SumTips. Contact | Sitemap | Privacy Policy