在Wordpress中上传期间重命名文件 [英] Rename files during upload within Wordpress

查看:72
本文介绍了在Wordpress中上传期间重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重命名与帖子标题匹配的上传文件名.

I am trying to rename upload filenames match the Post Title.

另一个线程显示了如何重命名为哈希:
在Wordpress后端上传期间重命名文件

This other thread shows how to rename to hash:
Rename files during upload within Wordpress backend

使用此代码:

function make_filename_hash($filename) {
    $info = pathinfo($filename);
    $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    $name = basename($filename, $ext);
    return md5($name) . $ext;
}
add_filter('sanitize_file_name', 'make_filename_hash', 10);

有人知道用于重命名文件以匹配Post Title.extension的代码吗?

Does anyone know the code to rename the file to match Post Title.extension?

推荐答案

barakadam的答案几乎是正确的,只是根据我在他的答案下方留下的评论进行了一些修正.

barakadam's answer is almost correct, just a little correction based on the comment I left below his answer.

function new_filename($filename, $filename_raw) {
    global $post;
    $info = pathinfo($filename);
    $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    $new = $post->post_title . $ext;
    // the if is to make sure the script goes into an indefinate loop
    if( $new != $filename_raw ) {
        $new = sanitize_file_name( $new );
    }
    return $new;
}
add_filter('sanitize_file_name', 'new_filename', 10, 2);

代码说明:

假设您将原始文件名为picture one.jpg的文件上传到名为我在巴黎/伦敦度假"的帖子.

Lets assume you upload a file with the original filename called picture one.jpg to a post called "My Holiday in Paris/London".

当您上传文件时,WordPress使用

When you upload a file, WordPress removes special characters from the original filename using the sanitize_file_name() function.

函数底部的右边是过滤器.

Right at the bottom of the function is where the filter is.

// line 854 of wp-includes/formatting.php
return apply_filters('sanitize_file_name', $filename, $filename_raw);

此时,$ filename将为picture-one.jpg.因为我们使用了add_filter(),所以将使用$ filename作为picture-one.jpg和$ filename_raw作为picture one.jpg来调用new_filename()函数.

At this point, $filename would be picture-one.jpg. Because we used add_filter(), our new_filename() function will be called with $filename as picture-one.jpg and $filename_raw as picture one.jpg.

然后,我们的new_filename()函数将文件名替换为带有附加原始扩展名的帖子标题.如果我们在此处停止,则新文件名$new最终将是My Holiday in Paris/London.jpg,我们所有人都知道这是无效的文件名.

Our new_filename() function then replaces the filename with the post title with the original extension appended. If we stop here, the new filename $new would end up being My Holiday in Paris/London.jpg which all of us know is an invalid filename.

这是当我们再次调用sanitize_file_name函数时.注意那里的条件语句.从$new != $filename_raw开始,它将尝试再次清理文件名.

Here is when we call the sanitize_file_name function again. Note the conditional statement there. Since $new != $filename_raw at this point, it tries to sanitize the filename again.

sanitize_file_name() 将被调用,并且在函数末尾,$filename将是My-Holiday-in-Paris-London.jpg,而$filename_raw仍将是My Holiday in Paris/London.jpg.由于apply_filters(),我们的new_filename()函数将再次运行.但是这次,因为$new == $filename_raw,那才是结束.

sanitize_file_name() will be called and at the end of the function, $filename would be My-Holiday-in-Paris-London.jpg while $filename_raw would still be My Holiday in Paris/London.jpg. Because of the apply_filters(), our new_filename() function runs again. But this time, because $new == $filename_raw, thats where it ends.

然后最终返回My-Holiday-in-Paris-London.jpg.

这篇关于在Wordpress中上传期间重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆