Change attachment filename

时间:2011-10-05 作者:FLX

是否有一个函数允许我根据我的附件ID更改附件的文件名?

谢谢丹尼斯

4 个回复
最合适的回答,由SO网友:Ijaas 整理而成

这将允许您在上传附件后立即重命名附件:

add_action(\'add_attachment\', \'rename_attachment\');
function rename_attachment($post_ID){

    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
        //dirname   = File Path
        //basename  = Filename.Extension
        //extension = Extension
        //filename  = Filename

    $newfilename = "NEW FILE NAME HERE";
    $newfile = $path[\'dirname\']."/".$newfilename.".".$path[\'extension\'];

    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );

}

SO网友:kaiser

用例

添加文件更新文件(是的,也适用于已经存在的文件)多个文件无使用情形wordpress自动执行的自动保存作业,或者如果未满足目标文件类型或mime类型,则会中止。

Goodies

您可以设置文件名、文件类型和;在foreach 环该文件获取帖子ID,然后附加附件ID,这样您可以一次安全地上载和更改多个文件。这还涉及到按(第一个)post ID和(第二个)附件ID对文件进行排序。

function wpse30313_update_attachment_names($post_ID)
{
    // Abort if WP does an autosave 
    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) 
        return;

    # >>>> SET
        // New file name:
        $new_file_name = "___";

        // Best would be to take the post name as file name instead of a custom title:
        # $post_data = get_post( $post_ID );
        # $new_file_name = $post_data->post_name;

        // The file types we want be changed:
        $allowed_types = array(
            \'image\'
        );

        // The mime types we want to be changed:
        $allowed_ext = array(
             \'jpg\'
            ,\'jpeg\'
            ,\'gif\'
            ,\'png\'
        );
    # <<<< SET

    // Appended by post ID for collision safety
    $new_file_name = "{$new_file_name}-{$post_ID}";

    // get all attached files
    $attachments = get_children( array( 
         \'post_type\'    => \'attachment\'
        ,\'post_parent\'  => $post_ID
    ) );

    // Bulk updating attached file names
    foreach ( $attachments as $att )
    {
        $att_ID     = $att->ID;
        // Append attachment ID (collision safety)
        // Also allows sorting files by post & then attchment ID
        $new_name   = "{$new_file_name}-{$att_ID}";

        $mime_type  = explode( "/", get_post_mime_type( $att->ID ) );
        $file_type  = $mime_type[0];
        $mime_type  = $mime_type[1];

        // Skip file types we don\'t want to change
        if ( ! in_array( $file_type, $allowed_types ) )
            continue;
        // Skip mime types we don\'t want to change
        if ( ! in_array( $mime_type, $allowed_ext ) )
            continue;

        // Get current file info
        $file_path = get_attached_file( $att->ID );
        $path   = pathinfo( $file_path );
        $dir    = trailingslashit( $path[\'dirname\'] );
        $ext    = $path[\'extension\'];

        // Build final name
        $final  = "{$dir}{$new_name}.{$ext}";

        // Skip if the path was already changed on upload
        // If we don\'t set this, the function wouldn\'t work for older files
        if ( $file_path == $final )
            continue;

        // Update attachment-post meta info for file
        rename( $file_path, $final );
        update_attached_file( $att_ID, $final );
    }

    return;
}
add_action( \'add_attachment\', \'wpse30313_update_attachment_names\' );
add_action( \'edit_attachment\', \'wpse30313_update_attachment_names\' );
该函数应添加到函数中。php文件或(更好)作为单独的小插件。只需在顶部添加插件注释,将其上载到插件文件夹并激活即可

SO网友:Naoise Golden

我会使用PHP的rename 和给定的文件路径get_attached_file.

function rename_file( $post_id, $newname ) {
    $file = get_attached_file( $post_id );
    rename($file,dirname($file).$newname)
}
请注意,这尚未测试,在处理文件时应格外小心。它可能需要改变才能发挥作用,但可能是一个很好的起点。希望这有帮助。

如果有帮助,请告诉我,我会将代码更改为实际工作代码。

SO网友:Mohit Bumb
add_action(\'add_attachment\', \'rename\');
function rename($post_ID){

    $post = get_post($post_ID);
    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
    $newfilename = "mynewfilename";
    $newfile = $path[\'dirname\']."/".$newfilename.".".$path[\'extension\'];

    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );

}

Reference http://codex.wordpress.org/Function_Reference/update_attached_filehttp://wordpress.org/tags/add_attachment

结束

相关推荐

how to edit attachments?

在将例如文件附加到帖子时,如何在事后编辑/删除它们?在帖子编辑器中找不到任何内容。谢谢