• Resolved goranpro

    (@goranpro)


    There is a function bellow in the FAQ that we can use to customize file names. My question is if i can use rand() to generate a filename in this function?

    Im asking because if this code is also used for something other than the generation the filename only ONCE before it is permanently written to db…for example to get the filename of the image multipel times in the same run …it would allways get a different value for the same image…

    So in short should this function return a deterministic value or can i use rand() in this function to generate random image names?

    add_filter( ‘mfrh_new_filename’, ‘my_filter_filename’, 10, 3 );

    function my_filter_filename( $new, $old, $post ) {
    return “renamed-” . $new;
    }

    • This topic was modified 7 years, 7 months ago by goranpro.
    • This topic was modified 7 years, 7 months ago by goranpro.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Jordy Meow

    (@tigroumeow)

    Well, it should be returning a deterministic value otherwise it will keep on popping that “Auto Rename” button, and the job of renaming images will be… never ending 🙂 You could do this but then you would need to “lock” the renaming.

    Another way to do this would be to generate a random “looking” filename depending on values which are available to you in the $post. Actually, you could get information about this file, like when it was written (the date), or a checksum, encode this somehow and generate a truly unique file based on those factors. That would be a deterministic function which produces a unique filename. I am sure that’s doable and I would be very interested to see a result. That could be even an interesting option for the plugin.

    Thread Starter goranpro

    (@goranpro)

    Hi,

    I made it realy simple taking the old filename and adding a hardcoded string to it. This is important to me because I want to be able to change this string to produce a different filename if needed. As I only need this for Woocommerce images that are formated in a way where the first part of the image name(seperated by dash) is a product/variable name, I only changed the second part of the filename.

    add_filter( 'mfrh_new_filename', 'my_filter_filename', 10, 3 );
    function my_filter_filename( $new, $old, $post ) {
      $dashCount = substr_count($old, "-");
      if ($dashCount == 1){
        $filenameArray = explode("-", $old);
        $firstPart = $filenameArray[0];
        $hashSeed = $old."ABC"; //deterministic seed
        $randCharHash = substr(sha1($hashSeed),17,6);
    
        return $firstPart."-".$randCharHash;
      }else{
    	  return $old;
      }
    }
    • This reply was modified 7 years, 7 months ago by goranpro.
    • This reply was modified 7 years, 7 months ago by goranpro.
    Plugin Author Jordy Meow

    (@tigroumeow)

    This code is actually quite interesting, and if you agree, I will share it on my tutorial about how to use this filter. It’s super useful for WooCommerce users who are looking for good and unique filenames of their photos.

    Thread Starter goranpro

    (@goranpro)

    Hi,

    sure you can but I think i found some erros in it.

    First is that if I use the $old variable for seed (i was thinking that this is always the original file name but its not) the “Auto Rename” is still there. So I had to use the $new variable for seed and then the “Auto Rename” is gone from the Media Library after I run the bulk rename. (im still not sure what those variables hold)

    And the second thing is that im not sure if the “dash” rule applies in general for Woocommerce or if it is unique for my setup of products…

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Should mfrh_new_filename return deterministic values?’ is closed to new replies.