In a function fired from filter wp_handle_upload_prefilter
, how can I utilise a variable obtained from previously firing action edit_user_profile
?
user-edit.php
, 当用户上载文件(ACF图像)时,获取表单主题用户的用户名(不是当前用户),并使用该用户名重命名文件。我目前的想法:
使用edit_user_profile
, 编辑用户加载时,将ID捕获到变量。
将其馈送到wp_handle_upload_prefilter
, 它进行文件重命名,以便生成所需的文件名字符串。
但是,我无法以不在媒体选择器模式中引发错误的方式使其正常工作。
选项?:我不确定我是否使用global
s是正确的。我更喜欢这种方法,因为它看起来最简单。
匿名函数和使用use
?
当上传预过滤器可能关注Ajax模式时,我推断我可以通过这种方式传递它们,这是完全错误的吗?我假设传递变量仍然可以。顺序重要吗?
$myuserid = 5; // global scope - set 5 just to test flow
// 1b. WHEN EDIT-USER.PHP LOADS, CAPTURE USER_ID
function custom_user_profile_fields( $profileuser ) {
$myuserid = $_GET[\'user_id\'];
echo \'<h1>Foo We are Setting $myuserid to \'.$myuserid.\'</h1>\';
}
// add_action( \'show_user_profile\', \'custom_user_profile_fields\' );
add_action( \'edit_user_profile\', \'custom_user_profile_fields\' );
// 2. WHEN TAKING AN UPLOAD, USE USER_ID TO GET OUR DATA & RENAME FILE
// Filter entry, cf. https://wordpress.stackexchange.com/questions/168790/how-to-get-profile-user-id-when-uploading-image-via-media-uploader-on-profile-pa
// p1: filter, p2: function to execute, p3: priority eg 10, p4: number of arguments eg 2
add_filter(\'wp_handle_upload_prefilter\', \'my_pre_upload\', 2, 1);
function my_pre_upload($file, $myuserid){ // was function my_pre_upload($file, $myuserid) with 2 arguments{
$user = get_userdata( $myuserid );
// Renaming, cf. https://stackoverflow.com/a/3261107/1375163
$info = pathinfo($file[\'name\']);
$ext = empty($info[\'extension\']) ? \'\' : \'.\' . $info[\'extension\'];
$name = basename($file[\'name\'], $ext);
$file[\'name\'] = $user->user_login . $ext;
// Carry on
return $file;
}