WordPress.org

Plugin Directory


Ignore:
Timestamp:
07/04/12 17:43:26 (5 years ago)
Author:
batmoo
Message:

Update to 0.4

  • New helper function
  • Support for WP.com infinite scroll
  • Prevent circular lazy-load references
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lazy-load/trunk/lazy-load.php

    r498772 r567472  
    33 * Plugin Name: Lazy Load 
    44 * Description: Lazy load images to improve page load times. Uses jQuery.sonar to only load an image when it's visible in the viewport. 
    5  * Version: 0.3 
     5 * Version: 0.4 
    66 * 
    77 * Code by the WordPress.com VIP team, TechCrunch 2011 Redesign team, and Jake Goldman (10up LLC). 
     
    1515class LazyLoad_Images { 
    1616 
    17     const version = '0.3'; 
     17    const version = '0.4'; 
    1818 
    1919    function init() { 
    2020        add_action( 'wp_enqueue_scripts', array( __CLASS__, 'add_scripts' ) ); 
    21         add_filter( 'the_content', array( __CLASS__, 'add_image_placeholders' ), 11 ); 
     21        add_filter( 'the_content', array( __CLASS__, 'add_image_placeholders' ), 99 ); // run this later, so other content filters have run, including image_add_wh on WP.com 
    2222        add_filter( 'post_thumbnail_html', array( __CLASS__, 'add_image_placeholders' ), 11 ); 
    2323    } 
     
    3333            return $content; 
    3434 
     35        // Don't lazy-load if the content has already been run through previously 
     36        if ( false !== strpos( $content, 'data-lazy-src' ) ) 
     37            return $content; 
     38 
    3539        // In case you want to change the placeholder image 
    3640        $placeholder_image = apply_filters( 'lazyload_images_placeholder_image', self::get_url( 'images/1x1.trans.gif' ) ); 
    3741 
    3842        // This is a pretty simple regex, but it works 
    39         $content = preg_replace( '#<img([^>]+?)src=[\'"]([^\'">]*)[\'"]([^>]*)>#', sprintf( '<img${1}src="%s" data-lazy-src="${2}"${3}><noscript><img${1}src="${2}"${3}></noscript>', $placeholder_image ), $content ); 
     43        $content = preg_replace( '#<img([^>]+?)src=[\'"]?([^\'"\s>]+)[\'"]?([^>]*)>#', sprintf( '<img${1}src="%s" data-lazy-src="${2}"${3}><noscript><img${1}src="${2}"${3}></noscript>', $placeholder_image ), $content ); 
    4044 
    4145        return $content; 
     
    4751} 
    4852 
     53function lazyload_images_add_placeholders( $content ) { 
     54    LazyLoad_Images::add_image_placeholders( $content ); 
     55} 
     56 
    4957LazyLoad_Images::init(); 
    5058 
Note: See TracChangeset for help on using the changeset viewer.