Thanks for your question and for your patience as I work through the topics that accumulated during my travels.
The mla_upload_prefilter hook is only called when items are added from the Media/Add New (Upload New Media) screen. It looks like you are uploading new items from the Post/Edit Post “Add Media” popup window or the “Image” block in the new editor to add an image to a post or page.
You can use the WordPress wp_handle_upload_prefilter hook to accomplish your goal. Here is some sample code wrapped in a small custom plugin:
<?php
/*
Plugin Name: Upload Prefilter Hook Example
Description: Alters the name of a file during upload
Author: David Lingren
Version: 1.00
*/
class UploadPrefilterHookExample {
public static function initialize() {
add_filter( 'wp_handle_upload_prefilter', 'UploadPrefilterHookExample::upload_prefilter', 10, 1 );
}
public static function upload_prefilter( $file ) {
error_log( 'UploadPrefilterHookExample::upload_prefilter $_REQUEST = ' . var_export( $_REQUEST, true ), 0 );
if ( ! empty( $_REQUEST['post'] ) ) {
// Gutenberg REST request
$parent = get_post( (int) $_REQUEST['post'] );
$post_name = $parent->post_title;
} elseif ( ! empty( $_REQUEST['post_id'] ) ) {
// Media Manager AJAX request
$parent = get_post( (int) $_REQUEST['post_id'] );
$post_name = $parent->post_title;
} else {
$post_name = 'No parent';
}
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$user_name = $user->display_name;
} else {
$user_name = 'Not logged in';
}
$pathinfo = pathinfo( $file['name'] );
$file['name'] = $user_name . ' - ' . $post_name . '.' . $pathinfo['extension'];
return $file;
} // upload_prefilter
} //UploadPrefilterHookExample
add_action('init', 'UploadPrefilterHookExample::initialize');
?>
You can copy the code into a stand-alone PHP file or add it to your theme’s functions.php file. You may need additional code to ensure that you are only changing the file names when you want to, or to perform other editing and validation.
I hope that gets you started on a solution for your application. I am marking this topic resolved, but please update it if you have any problems or further questions regarding the above suggestions. Thanks for your interest in the plugin.