Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update - Lazy load for iframe
  • Loading branch information
jogeshpi03 committed Jan 20, 2020
1 parent 24c7a3a commit ad7fd31
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,5 +1,5 @@
# Omni Lazy Load
The best plugin to for Images to boost WordPress Performance.
The best plugin to for Images and IFrames to boost WordPress Performance.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion omni-lazy-load.php
Expand Up @@ -7,7 +7,7 @@
* @wordpress-plugin
* Plugin Name: Omni Lazy Load
* Plugin URI: https://webomnizz.com/wordpress-plugins/omni-lazy-load-image/
* Description: WordPress lazy loader for Images to boost your website performance.
* Description: WordPress lazy loader for Images and IFrames to boost your website performance.
* Version: 1.0.0
* Author: Jogesh
* Author URI: https://webomnizz.com/
Expand Down
56 changes: 45 additions & 11 deletions public/class-omni-lazyload-public.php
Expand Up @@ -95,22 +95,56 @@ public function the_content_filter( $content ) {
$dom = new DOMDocument();
@$dom->loadHTML($content);

// Apply Lazy Image
foreach ($dom->getElementsByTagName('img') as $node) {
$this->lazy_image( $node );
}

$oldsrc = $node->getAttribute('src');
$old_srcset = $node->getAttribute('srcset');
$old_classes = $node->getAttribute('class');

$node->setAttribute("data-src", $oldsrc );
$node->setAttribute("data-srcset", $old_srcset);

$node->removeAttribute("srcset");
$node->removeAttribute("src");

$node->setAttribute("class", trim($old_classes) . " lazyload");
// Apply Lazy IFrame
foreach ($dom->getElementsByTagName('iframe') as $node) {
$this->lazy_iframe( $node );
}

$newHtml = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
return $newHtml;
}

/**
* Apply Lazyloading on image
*
* @param object $node
* @param string $class
*
* @since 1.0.0
* @return void
*/
private function lazy_image( $node, $class = 'lazyload' ) {

$oldsrc = $node->getAttribute('src');
$old_srcset = $node->getAttribute('srcset');
$old_classes = $node->getAttribute('class');

$node->setAttribute("data-src", $oldsrc );
$node->setAttribute("data-srcset", $old_srcset);

$node->removeAttribute("srcset");
$node->removeAttribute("src");

$node->setAttribute("class", trim($old_classes) . " {$class}");
}

/**
* Apply Lazyloading on iframe
*
* @param object $node
* @param string $class
*
* @since 1.0.0
* @return void
*/
private function lazy_iframe( $node, $class = 'lazyload' ) {

$old_classes = $node->getAttribute('class');
$node->setAttribute("class", trim($old_classes) . " {$class}");
}
}

0 comments on commit ad7fd31

Please sign in to comment.