朱纯树博客
VPS测评推荐网站
cloudacead cloudacead

WordPress两种方法实现上传媒体图片文件自动重命名

我们发布文章时,会上传一些图片、音频之类的文件。但是WordPress没有自动 给新上传文件重命名的功能,逐个文件去重命名那就太麻烦了,那么我们改如何自动给上传的媒体文件图片重命名呢?

本站在网站搜索了些上WordPress上传媒体文件自动重命名的WordPress教程,说法不一,方法也多种多样,于是整理了下,希望能方便大家以后使用。

方法一: 按上传时间自动重命名

//WordPress上传文件重命名
function git_upload_filter($file) {
	$time = date("YmdHis");
	$file['name'] = $time . "" . mt_rand(1, 100) . "." . pathinfo($file['name'], PATHINFO_EXTENSION);
	return $file;
}
add_filter('wp_handle_upload_prefilter', 'git_upload_filter');

二、用MD5加密生成数字并重命名

//WordPress上传文件重命名
function rename_filename($filename) {
    $info = pathinfo($filename);
    $ext = emptyempty($info['extension']) ? '' : '.' . $info['extension'];
    $name = basename($filename, $ext);
    return substr(md5($name), 0, 20) . $ext;
}
add_filter('sanitize_file_name', 'rename_filename', 10);

使用方法

将上面两段代码其中一段添加到主题的functions.php 文件中即可。

文章来源于互联网:WordPress两种方法实现上传媒体图片文件自动重命名

赞(0) 打赏
未经允许不得转载:VPS测评推荐网站 - 朱纯树博客 » WordPress两种方法实现上传媒体图片文件自动重命名

评论 33

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #45

    Greetings from California! I’m bored to death at work so I decided to browse your site on my iphone
    during lunch break. I really like the knowledge you provide here and can’t
    wait to take a look when I get home. I’m amazed at
    how quick your blog loaded on my mobile .. I’m not even using WIFI,
    just 3G .. Anyhow, very good site!

  2. #44

    Attractive section of content. I just stumbled upon your website and in accession capital to assert that I get actually enjoyed account your blog posts.
    Any way I will be subscribing to your augment and even I achievement you access consistently fast.

  3. #43

    Do you mind if I quote a couple of your posts as long as I provide credit and sources
    back to your site? My blog is in the very same
    area of interest as yours and my users would certainly benefit
    from some of the information you provide here. Please let me know if this ok with you.
    Thank you!

    Look into my site; special