• Resolved Generosus

    (@generosus)


    Good Day,

    We’re using your plugin on a staging site. So far, so good.

    Question: How can we exclude all images contained in Events, Portfolio Items, and Posts from being lazy loaded?

    Helpful: The developers of Perfmatters shared the code snippet below for their plugin (lazy loading feature). Was hoping you can come up with a similar one.

    add_filter('perfmatters_lazyload', function($lazyload) {
    if(is_singular(array('post', 'avada_portfolio', 'tribe_events'))) {
    return false;
    }
    return $lazyload;
    });

    Note: A code snippet like the one above would serve us better over excluding each post (i.e., post URL) via your exclusions filter. We have too many of them.

    Thank you!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support iSaumya

    (@isaumya)

    Hi @generosus,
    Unlike Perfmatters which works at the page level, Optimole works at the image level. So, you can take advantage of optml_dont_replace_url filter to exclude certain URLs from lazy loading as pemtioned here: https://docs.optimole.com/article/1191-exclude-from-optimizing-or-lazy-loading

    Thread Starter Generosus

    (@generosus)

    Hey @isaumya,

    Ah, makes more sense now. Thank you.

    Concerning your filter (below), how can we modify it for multiple images?

    add_filter('optml_dont_replace_url', function( $old, $url ) {
    if ( $url === 'https://example.com/wp-content/uploads/2018/09/1.jpg' ) {
    return true;
    }
    return $old;
    }, 10, 2);

    Again, thank you.

    Plugin Support iSaumya

    (@isaumya)

    Hi @generosus,
    You can either create an array of URLs and then check the url inside that array to achieve what you are looking for.

    Alternatively you can take advantage of the exclusion section in the plugin settings page: https://docs.optimole.com/article/1191-exclude-from-optimizing-or-lazy-loading#exclusions

    Thread Starter Generosus

    (@generosus)

    Hi @isaumya,

    So, as an example, what would the above filter look like for excluding three (3) image URLs?

    Thank you.

    Plugin Support iSaumya

    (@isaumya)

    Hi @generosus,
    You can go inside the plugin settings > exclusions section and add new exclusion saying don’t lazyload if filename contains some-text.

    Here’s a screenshot:

    Thread Starter Generosus

    (@generosus)

    Hey @isaumya,

    Yes, we know that part. We don’t want to use your plugin’s built-in exclusions, we want to use the PHP code snippet you provided — which is good only for one (1) image.

    So, to clarify further, can you provide a single code snippet that will allow us to exclude three (3) images. You may need to consult your developers.

    Will this work?

    add_filter('optml_dont_replace_url', function( $old, $url ) {
    if ( $url === 'https://example.com/wp-content/uploads/image1.jpg' );
    if ( $url === 'https://example.com/wp-content/uploads/image2.jpg' );
    if ( $url === 'https://example.com/wp-content/uploads/image3.jpg' ) {
    return true;
    }
    return $old;
    }, 10, 2);

    Thank you.

    Plugin Support iSaumya

    (@isaumya)

    Hi @generosus,
    If you have multiple image URLs parts for which you want to bypass lazy loading you can do something like this:

    add_filter('optml_dont_replace_url', function($old, $url) {
      // URL parts for which lazyload will be disabled
      $url_parts = [
        'uploads/image1',
        'uploads/image2',
        'uploads/image3',
      ];
    
      // Loop through the URL parts to check if the current URL has the URL parts in them - if so return true else return old url
      foreach ($url_parts as &$part) {
        if (stripos($url, $part) !== false) {
          // URL contains one of the parts. 
          return true;
        }
      }
    
      return $old;
    }, 10, 2);

    Hope this helps. Please consider searching the web about how to write PHP code foor custom usecases like these.

    Thread Starter Generosus

    (@generosus)

    Hey @isaumya,

    Thank you. Just what the doctor ordered ??

    It would be great if you can add your code snippet to your documentation (to help the community).

    Cheers!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to Exclude Posts from Lazy Loading?’ is closed to new replies.