• Resolved Peter

    (@digitalform)


    Hi Jordy,

    This is how I want to rename the files after uploading them (and in bulk):

    ACF field – Title – ID

    For example:
    sale-this-is-property-title-1458__01.jpg

    I tried to use this code:

    function custom_filter_filename($new, $old, $post) { 
    	
    	$p_id = $post['ID'];
    	$p_title = $post['post_name'];
    	$buy_rent = get_field('buy_rent', $p_id);
    	
    	return $buy_rent.'-'.$p_title.'-'.$p_id.'.jpg';
    }
    add_filter('mfrh_new_filename', 'custom_filter_filename', 10, 3);

    I use the PRO version.

    Best regards,
    Peter

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Jordy Meow

    (@tigroumeow)

    Hi @digitalform,

    This is not working? Sorry, I missed the question ??

    That said, I think this would be better:

    $p_title = $new

    The title will use the new name generated by Media File Renamer, which might be the beautified title of the attached product.

    Thread Starter Peter

    (@digitalform)

    Dear Jordy,

    What is the $new, $old and $post?

    I saw a snipet: $post_date = strtotime( $post['post_date'] );
    I think I can recover the post ID by $post: $p_id = $post['ID'];
    And an ACF field: $buy_rent = get_field('buy_rent', $p_id);

    And from these I want to give a unique file name to the images.

    $buy_rent.'-'.$p_title.'-'.$p_id.'.jpg';

    E.g. sale_property_title_5455.jpg

    Is it possible with the “mfrh_new_filename” hook?

    Best regards,
    Peter

    • This reply was modified 2 years ago by Peter.
    Plugin Author Jordy Meow

    (@tigroumeow)

    Yes, it’s exactly how you should do it ??

    $new: the new filename proposed by Media File Renamer (without extension)
    $old: the previous filename (without extension)
    $post: you know, you used it, it’s the object containing the data of the post

    So that should work fine.

    Thread Starter Peter

    (@digitalform)

    Hello Jordy,

    Thanks for the answer.
    But the code below doesn’t work.

    function custom_filter_filename($new, $old, $post) { 
    	
    	$p_id = $post['ID'];
    	$p_title = $post['post_name'];
    	$buy_rent = get_field('buy_rent', $p_id);
    	
    	return $buy_rent.'-'.$p_title.'-'.$p_id.'.jpg';
    }
    add_filter('mfrh_new_filename', 'custom_filter_filename', 10, 3);

    I uploaded a picture. The name is Marbella-Property.jpg.
    The plugin renamed it to: marbella-property.jpg

    But that’s not good. This would be the correct new file name: sale-marbella-property-1458.jpg

    The 1458 is the post ID.

    Is my PHP code wrong?

    Kind regards,
    Peter

    Plugin Author Jordy Meow

    (@tigroumeow)

    Where did you add this code? Have you tried using Code Snippets?

    Thread Starter Peter

    (@digitalform)

    Hello Jordy,

    In the functions.php.

    Your tutorial code works:

    // Override the filename naming rules with this filter
    add_filter( 'mfrh_new_filename', 'my_filter_filename', 10, 3 );
    
    function my_filter_filename( $new, $old, $post ) {
      
      // Get the date from the $post (which is the media entry) and convert it to a timestamp
      $post_date = strtotime( $post['post_date'] );
      
      // Make this timestamp to looks like 2018-01-01
      $formatted_date = date('Y-m-d', $post_date);
      
      // Add this timestamp in front of the filename already decided by Media File Renamer
      $new_filename = $formatted_date . "-" . $new;
      
      return $new_filename;
    }

    But not mine. ??

    – Peter

    Plugin Author Jordy Meow

    (@tigroumeow)

    Please try with Code Snippets. Sometimes there are issues of filters being added too early or too late with the functions.php ??

    Plugin Author Jordy Meow

    (@tigroumeow)

    Also, check your Error Logs, just in case.

    Thread Starter Peter

    (@digitalform)

    Meanwhile, I realized that $post is not the post (property post_type), but the media file. ??
    That’s why it didn’t work.

    Best regards,
    Peter

    Plugin Author Jordy Meow

    (@tigroumeow)

    Please contact me directly: https://meowapps.com/contact. I can make a version that will pass also the related post ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Rename file by ACF field, title and ID’ is closed to new replies.