我想将附件从WP uploads文件夹中的一个目录移动到另一个目录。下面的代码完成了任务。如您所见,我移动了原始文件以及由WP自动生成的所有widthxheight文件(例如150x150、30x300)。我调用update\\u attached\\u file($id,$new\\u filepath),我可以看到移动的文件,它们在媒体库中可见-没有问题。
当我尝试重命名原始文件时,会出现此问题。从“name.jpg”到“username\\u name.jpg”。现在这些文件仍被移动到新目录,并被重命名为“username\\u name.jpg”、“username\\u name-150x150.jpg”、“username\\u name-300x300.jpg”。However, WP Media library现在不显示缩略图。它仍然希望“name-150x150.jpg”是“username\\u name-150x150.jpg”的istead。
问题是如何更新wordpress中widthxheight文件的新文件名?我搜索了,但找不到任何可以为附件大小文件提供ID的函数。这一定是可能的。这么多插件一直在重命名。。。有什么帮助吗?
// Get all uploaded file attachment IDs
$media_ids = $_POST[\'item_meta\'][205]; // file upload field ID
// Loop through each attachment
foreach ( (array)$media_ids as $id ) {
if ( ! $id ) {
continue; // exit when there are no more attachments to loop through
}
// extract attachment file name without extension
$path_parts = pathinfo( get_attached_file( $id ));
$filenamenoextention = $path_parts[\'filename\'];
// Extract username
$username = sanitize_title($_POST[\'item_meta\'][195]);
// Set new base path
$new_filepath_base = $_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-content/uploads/profiles/pro/\';
// create user directory, if it does not exist already
$userdir = $new_filepath_base . $username;
if (!file_exists($userdir)) {
mkdir($userdir, 0775, true);
}
// Wordpress saves extra file sizes. Extract paths to all of them using wildcard *.
$original_filepaths = glob($_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-content/uploads/form/20/\' . $filenamenoextention . \'*\');
// Loop through WP-generated files and move them to a new location.
foreach ($original_filepaths as $original_filepath) {
// Add alt to the image. Good for SEO.
update_post_meta( $id, \'_wp_attachment_image_alt\', \'main profile photo - \' . $username);
// enrich filename with username and a tag
// $new_filename = $username .\'_\' . basename($original_filepath);
$new_filename = basename($original_filepath);
$new_filepath = $userdir . \'/\' . $new_filename;
$rename_result = rename($original_filepath, $new_filepath);
// If move was successful, update file path so that WP Media interface can find it.
if($rename_result)
{
update_attached_file($id, $new_filepath );
}
}
}
SO网友:Tanya
下面是有效的代码。文件将移动到“username”文件夹,并更新所有文件的名称(包括宽度x高度)。
// Get all uploaded file attachment IDs
$media_ids = $_POST[\'item_meta\'][205]; // file upload field ID
// Loop through each attachment
foreach ( (array) $media_ids as $id ) {
if ( ! $id ) {
continue; // exit when there are no more attachments to loop through
}
// Extract username
$username = sanitize_title($_POST[\'item_meta\'][195]);
// Set filenames and paths
$old_filename_mainmedia = basename( get_attached_file( $id ) );
$new_filename_mainmedia = $username . \'_\' . $old_filename_mainmedia;
$old_filepath_mainmedia = $_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-content/uploads/\' . $old_filename_mainmedia;
$new_filepath_mainmedia = $_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-content/uploads/profiles/\' . $username .\'/\' . $new_filename_mainmedia;
// create user directory, if it does not exist already
if (!file_exists($_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-content/uploads/profiles/pro/\' . $username)) {
mkdir($_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-content/uploads/profiles/pro/\' . $username, 0775, true);
}
// Construct attachment metadata: https://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata
$meta = wp_get_attachment_metadata($id);
$meta[\'file\'] = str_replace( $old_filename_mainmedia, $new_filename_mainmedia, $meta[\'file\'] );
// Rename the original file
rename($old_filepath_mainmedia, $new_filepath_mainmedia);
// Rename the sizes
$old_filename_sizemedia = pathinfo( basename( $old_filepath_mainmedia ), PATHINFO_FILENAME );
$new_filename_sizemedia = pathinfo( basename( $new_filepath_mainmedia ), PATHINFO_FILENAME );
foreach ( (array)$meta[\'sizes\'] AS $size => $meta_size ) {
$old_filepath_sizemedia = dirname( $old_filepath_mainmedia ).DIRECTORY_SEPARATOR.$meta[\'sizes\'][$size][\'file\'];
$meta[\'sizes\'][$size][\'file\'] = str_replace( $old_filename_sizemedia, $new_filename_sizemedia, $meta[\'sizes\'][$size][\'file\'] );
$new_filepath_sizemedia = dirname( $new_filepath_mainmedia ).DIRECTORY_SEPARATOR.$meta[\'sizes\'][$size][\'file\'];
rename( $old_filepath_sizemedia, $new_filepath_sizemedia );
}
// Add alt to the image. Good for SEO.
update_post_meta( $id, \'_wp_attachment_image_alt\', \'Gritmatch Pro - main profile photo - \' . $username);
// Update media library post title
$post = get_post($id);
$post->post_title = $new_filename_mainmedia;
$post->post_excerpt = "this is caption";
$post->post_content = "this is content";
$post->post_name = "this is post name";
wp_update_post( $post );
// Update Wordpress
wp_update_attachment_metadata( $id, $meta );
update_attached_file($id, $new_filepath_mainmedia );
} // end looping through each media attachment