似乎还无法找到正确的筛选器来处理此问题(编辑表单正在保存的位置),尤其是因为attachment_fields_to_edit
可在多个地方使用。但也许你已经知道了?无论如何,这会给你一个好的开始。。。
add_filter(\'??attachment_fields_save_filter??\',\'move_attachment_directory\',10,2);
function move_attachment_directory($form_fields,$post) {
$attach_id = $post->ID;
if (!isset($_POST[\'attachments\'][$attach_id][\'my_select\'])) {return;}
if ($_POST[\'attachments\'][$attach_id][\'my_select\'] == \'no\') {return;}
// set directories
$uploaddir = wp_upload_dir();
$newdirectory = \'new-directory\'; // or whatever it is
// get attachment metadata
$attachdata = wp_get_attachment_metadata($attach_id);
// move original image
$filepath = trailingslashit($uploaddir).$attachdata[\'file\'];
$newfilepath = trailingslashit($uploaddir).trailingslashit($newdirectory).$attachdata[\'file\'];
rename($filepath,$newfilepath);
$attachdata[\'file\'] = trailingslashit($newdirectory).$attachdata[\'file\'];
// move each attachment size too
foreach ($attachdata[\'sizes\'] as $size => $data) {
$file = $data[\'file\'];
$filepath = trailingslashit($uploaddir).$data[\'file\'];
$newfilepath = trailingslashit($uploaddir).trailingslashit($newdirectory).$data[\'file\'];
rename($filepath,$newfilepath);
$data[\'file\'] = trailingslashit($newdirectory).$data[\'file\'];
$attachdata[\'sizes\'][$size] = $data;
}
// update attachment metadata
wp_update_attachment_metadata($attach_id,$attachdata);
}
注意:如果您将媒体按月/年进行排序,我认为这不会像现在这样起作用。
在任何情况下,这都是未经测试的代码,可能有路径错误等,因此可能需要在测试环境中进行一些工作,但它表明这是可以完成的,希望它能让您进入下一步。。。