title: Renaming attachment files to the post title it's being added to in Wordpress
body: <p>Like the title says, I am trying to rename media gallery uploads to the title of the initial post the file is being added to.</p>
  
  <p>I found the following function and modified it to change the filename to the file's author slug</p>
  
  <pre><code>// file renamer (used for EDL images)
  function file_renamer( $filename ) {
    $info = pathinfo( $filename );
    $ext  = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
    $name = basename( $filename, $ext );
    global $current_user;
    get_currentuserinfo();
  
    return   $current_user-&gt;user_login . $ext;
  }
  </code></pre>
  
  <p>However- that wasn't good enough for the client. I haven't found any solutions to this yet online. Any help would be greatly appreciated!</p>
  
  <p>Edit - Just want to add that I am using advanced custom fields for this as well. Here is the rest of the code for this process, which directs specific custom field images to a certain directory-</p>
  
  <pre><code>// EDL uploaded images will go to the /edl directory in uploads for these fields.
  add_filter('acf/upload_prefilter/name=edl_dealer_logo', 'field_name_upload_prefilter');
  add_filter('acf/upload_prefilter/name=edl_image', 'field_name_upload_prefilter');
  add_filter('acf/upload_prefilter/name=edl_gallery_image', 'field_name_upload_prefilter');
  
  function field_name_upload_prefilter($file) {
  // in this filter we add a WP filter that alters the upload path
  add_filter( 'sanitize_file_name', 'file_renamer', 10 );
  add_filter('upload_dir', 'field_name_upload_dir');
  return $file;
  }
  // second filter
    function field_name_upload_dir($uploads) {
  
    $mydir = '/edl';
    // here is where we later the path
    $uploads['path'] = $uploads['basedir'].$mydir;
    $uploads['url'] = $uploads['baseurl'].$mydir;
  
    $uploads['subdir'] = '/edl';
    return $uploads;
  }
  </code></pre>