• Resolved publicradio

    (@publicradio)


    I’m using ImgIX to serve the images on my website. ImgIX caches all images, so if you update an image with the same filename, ImgIX will serve the old image.

    You can purge the cache for a single image using curl or PHP. I have a PHP script that will purge the image, so all I need to do now is hook into the Enable Media Replace plugin.

    My function is imgix_purge($filename), and I have it in my own custom plugin. I want my plugin to ‘listen’ for a media replacement where the user chooses to keep the existing filename. Then, I need to get the filename (and relative path) and pass it as an argument to my function.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Gerard Blanco

    (@sixaxis)

    Hi @publicradio!

    Can you please send us a message requesting assistance via this contact form and make sure you mention this ticket?

    Thank you!

    Thread Starter publicradio

    (@publicradio)

    Hi there, I did send the message, received a helpful response, and my function worked! Here’s what I did, in case this is useful to anyone else:

    
    add_action('enable-media-replace-upload-done', function($url){
      $headers = array(
          'Content-Type:application/json',
          'Authorization: Basic '. base64_encode('YOUR IMGIX API KEY')
      );
      $payload = json_encode(array("url" => $url));
      $curl = curl_init('https://api.imgix.com/v2/image/purger');
      curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($curl, CURLOPT_TIMEOUT, 30);
      curl_setopt($curl, CURLOPT_POST, 1);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      $response = curl_exec($curl);
      curl_close($curl);
      return $response;
    });
    

    And it works great. By the time the page reloads, ImgIX has already purged the old image and displays whatever you just uploaded. Very useful.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Can I trigger an action after replacement’ is closed to new replies.