如何更新有关重命名文件的WP

时间:2018-05-03 作者:Tanya

我想将附件从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 );

        }
      }

    }

2 个回复
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

SO网友:user141080

如果您在wordpress后端上载图像,wordpress将创建不同大小的文件副本(“widthxheight”文件)。该附件的元数据将保存在表“wp\\U Posteta”中。

但如果您查看此表,就会发现该附件只有两个条目(post\\u id)。第一个条目是“wp\\u attached\\u file”,第二个条目是“wp\\u attachment\\u metadata”。

_wp\\u attached\\u文件包含原始图像的路径

_wp\\u attachment\\u metadata包含元数据,其中包括“widthxheight”文件的名称和大小。

要从wp\\U attachment\\U metadata获取数据,可以使用以下功能wp_get_attachment_metadata 要更改数据,可以使用函数wp_update_attachment_metadata.

功能update_attached_file 仅编辑wp\\u attached\\u文件条目。

有用链接:

How to change _wp_attachment_metadata specific value in SQL? - wordpress.stackexchange.com

Change attachment filename - wordpress.stackexchange.com

结束

相关推荐

当子目录中有.htaccess或php.ini时,排除目录的.htaccess重写条件不起作用

我在服务器根目录中安装了WordPress(public_html) 对于我的一个客户,还有一个子目录,其中包含另一个程序员开发的脚本,他们仍在使用。问题是这个子目录有一个简单的.htaccess 具有密码保护的文件,以及php.ini 具有register_globals = on (是的,我知道……)。问题是,如果存在其中一个或两个文件,则RewriteCond %{REQUEST_FILENAME} !-d 条件不起作用,并重定向到404 WordPress页面。WordPress.htaccess