Enable Media Replace
[resolved] Add a hook for CloudFlare file purge (10 posts)

  1. rubious
    Posted 2 years ago #

    I am coming across a problem since I use this plugin along with CloudFlare; my images are being cached so when I replace an image it looks as if nothing happened.

    The solution is to purge the file after replacing it, however this can be laborious. I can use the API to do this automatically however I'm not sure if you have a hook I can add an action to, which is triggered after the file has been replaced?

    I'm thinking of something like this: http://www.bhagwad.com/blog/2013/technology/using-php-with-wordpress-to-purge-a-single-file-from-cloudflare.html/

    Please advise if there is currently a hook, or if you could add it to a future version? Thanks.


  2. Måns Jonasson
    Plugin Author

    Posted 2 years ago #

    I'm not sure how to add this functionality in a way that makes it work regardless of what CDN you happen to be using.

    Does anybody know of the best WP way to purge an item from a CDN cache?

  3. rubious
    Posted 2 years ago #

    I just need somewhere in your code that I can hook into which runs after a file has been replaced. So as soon as the file has been replaced I can add in some custom code to contact CloudFlare and purge the file.

  4. Måns Jonasson
    Plugin Author

    Posted 2 years ago #

    I have to admit I don't know exactly how.

    Would you test it for me? If you add:

    do_action("enable-media-replace-upload-done", $new_guid);

    after line 157 in upload.php (the last $returnurl statement) - does that do it for you?

  5. rubious
    Posted 2 years ago #

    That does work in that it fires the hook, but $new_guid is empty. I will need the full URL of the new file so this is important. Do you have any idea why this is not working?

    I've done the following:

    Line 158 of upload.php:
    do_action("enable_media_replace_upload_done", $new_guid);

    in my functions.php:

    function upload_done_function( $new_guid )
    	$email = 'my@email-address.com';
    	wp_mail( $email, "File changed", 'I just changed the following file: ' . $new_guid  . ' I will now tell CloudFlare to purge it.' );
    add_action( 'enable_media_replace_upload_done', 'upload_done_function', 10, 1 );
  6. Måns Jonasson
    Plugin Author

    Posted 2 years ago #

    I see, $new_guid is empty if you do a simple replace, without renaming the file. My mistake. Try this line instead:

    do_action("enable-media-replace-upload-done", ($new_guid ? $new_guid : $current_guid));

  7. rubious
    Posted 2 years ago #

    That worked a treat, thanks. Will you be adding that to the next version? :)

  8. Måns Jonasson
    Plugin Author

    Posted 2 years ago #


    Yes, I'll go ahead and check in the latest version now, you should have it live within the hour. Thanks for the input!

  9. rubious
    Posted 2 years ago #


    I actually just realised that it would be more useful to get the ID of the attachment, instead of the guid. This is so I can purge all the generated image sizes as well as the main image.

    Sorry to ask after you've already published it, but perhaps you could change it to:

    do_action("enable-media-replace-upload-done", $_POST["ID"]);

  10. rubious
    Posted 2 years ago #

    Hi Mans, do you think you could make this change? Or add another hook for the ID? It's more useful than the GUID as I can use it to replace all image sizes. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Enable Media Replace
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic


No tags yet.