Support » Plugin: EWWW Image Optimizer » Best way to use the plugin for images loaded through custom AJAX requests

  • Resolved therealgilles

    (@chamois_blanc)


    Hello, I am using ajax to load content generated through a plugin shortcode that contains images. Unfortunately that plugin does not set the sizes css parameter correctly.

    What is the best way to allow ewww-optimizer to process these images and fix up the missing sizes parameter (using data-sizes=”auto”)?

    Thank you 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter therealgilles

    (@chamois_blanc)

    I think I can simply call ewww_image_optimizer_filter_page_output() on the content. Would that be correct?

    Plugin Author nosilver4u

    (@nosilver4u)

    Normally, that might work, but if it’s going through admin-ajax.php, then I don’t think that will make a difference. The plugin generally blocks admin-ajax.php parsing, because it triggers the is_admin() check.

    What you probably need is to use the ‘eio_allow_admin_lazyload’ filter:
    add_filter( ‘eio_allow_admin_lazyload’, ‘__return_true’ );

    Put that somewhere connected to your plugin shortcode. If it’s a custom plugin, you can just add it directly in the shortcode function. Otherwise, if it’s a plugin available here on wordpress.org, we can look at adding support directly into the lazy loader itself.

    Thread Starter therealgilles

    (@chamois_blanc)

    Thank you so much for getting back to me, really appreciate it.

    I had looked through the code and figured that out. I implemented the filter following the examples in the code, so in a more target way:

    // Allow lazy load on load_content ajax requests
    add_filter('eio_allow_admin_lazyload', 'my_eio_allow_admin_lazyload', 15, 1);
    function my_eio_allow_admin_lazyload( $allow ) {
      if ( ! empty( $_POST['action'] ) && 'load_content_ajax' === $_POST['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification
        return true;
      }
    
      return $allow;
    }

    It seems to be working great so far.

    I wanted to add that I think this auto-sizes attribute functionality of the plugin should be advertised more, as many plugins don’t do this properly for their images. Great job on coming up with this idea and making it happen!

    Plugin Author nosilver4u

    (@nosilver4u)

    That’s great, glad you got it worked out so well!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Best way to use the plugin for images loaded through custom AJAX requests’ is closed to new replies.