WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. rubious
    Member
    Posted 1 year 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.

    http://wordpress.org/extend/plugins/enable-media-replace/

  2. Måns Jonasson
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Posted 1 year 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
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Posted 1 year 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
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Posted 1 year ago #

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

  8. Måns Jonasson
    Member
    Plugin Author

    Posted 1 year ago #

    Great!

    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
    Member
    Posted 1 year ago #

    Hi,

    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
    Member
    Posted 11 months 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

About this Topic

Tags

No tags yet.