kevinweber / lazy-load-for-videos

Speed up your site by replacing embedded Youtube and Vimeo videos with a clickable preview image. Free WordPress plugin.
http://wordpress.org/plugins/lazy-load-for-videos/
GNU General Public License v3.0
41 stars 23 forks source link

Improve embed detection; new filter for when to load scripts #25

Closed kevinweber closed 5 years ago

kevinweber commented 5 years ago

Improve embed detection

// For pages with multiple posts (e.g. homepage and archives),
// iterate over all posts to see if any of them includes an embed.
global $posts;
foreach($posts as $post) {
    $has_post_embed = $lazyload_videos_general->has_post_or_page_embed($post->ID);
    if ($has_post_embed) return true;
};

New filter: lazyload_videos_should_scripts_be_loaded

You can override when the JS and CSS of this plugin should be loaded. If you always return true as shown in the example below, the scripts will always be loaded, no matter if the page has a video embed or not.

function custom_theme_plugin__should_scripts_be_loaded($value) {
    // return $value;
    return true; // <- Always load scripts, no matter what page you're on
}
add_filter( 'lazyload_videos_should_scripts_be_loaded', 'custom_theme_plugin__should_scripts_be_loaded');

Imagine the following scenario:

  1. User navigates to page without video. No lazy video script is loaded.
  2. The site uses Ajax to load posts and pages without a page refresh.
  3. The user navigates to a different post that includes a video.
  4. The video doesn't show because the video script isn't loaded.

This can be avoided by loading the lazy video script during the initial page load, no matter if the initial page has a video or not. Above filter allows you to achieve exactly that.