Support » Plugin: Enable Media Replace » Add a hook for CloudFlare file purge

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Måns Jonasson


    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?

    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.

    Plugin Author Måns Jonasson


    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?

    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 = '';
    	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 );

    Plugin Author Måns Jonasson


    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));

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

    Plugin Author Måns Jonasson



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


    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”]);

    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!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Add a hook for CloudFlare file purge’ is closed to new replies.