朱纯树博客
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两种方法实现上传媒体图片文件自动重命名

评论 39

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

    The latest form of these drugs third generation anastrozole, letrozole, and exemestane are currently being used as a treatment for breast cancer Mitwally 2004 cialis generic name A direct link between smoking and higher blood pressure has not been established

    Inefeby2周前 (03-12)回复
  2. #44

    Hey There. I found your weblog using msn. This is a very
    well written article. I will make sure to bookmark
    it and come back to learn more of your helpful information. Thanks for the post.

    I’ll certainly return.

    Feel free to surf to my site – tracfone 2022

    tracfone 20224个月前 (12-02)回复
  3. #43

    I simply couldn’t depart your web site before suggesting that I really loved the standard information a person provide to your visitors?

    Is going to be back frequently to investigate cross-check new posts

    Also visit my page; tracfone

    tracfone4个月前 (11-26)回复
  4. #42

    Appreciation to my father who informed me about this blog, this blog is
    actually amazing.

  5. #41

    Thanks very interesting blog!

  6. #40

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

    Here is my web page: 2022

    20227个月前 (09-03)回复
  7. #39

    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!

    http://tinyurl.com/7个月前 (08-17)回复
  8. #38

    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.

    http://tinyurl.com/7个月前 (08-13)回复
  9. #37

    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

    special8个月前 (08-11)回复