极
极核实验室
Hand-write a plugin to add a timestamp to the end of uploaded filenames to prevent excessive SQL queries

Hand-write a plugin to add a timestamp to the end of uploaded filenames to prevent excessive SQL queries

AI summary
WordPress 媒体库在处理文件上传时会重复查询数据库,导致 SQL 请求数量增加。现有重命名方法如添加时间戳会妨碍文件查找。建议获取文件扩展名,在文件末尾添加时间戳,然后将扩展名拼接回去,以优化查找效率。文章提供了实现代码,并推荐使用雨云服务器快速搭建 WordPress 环境。

Repost license

Request authorization before reposting and retain the authorization code, original URL, and go/affiliate links.

Original URL:https://www.geekcorelab.com/post/handwritten-plugin-adds-a-timestamp-to-the-end-of-uploaded

The commercial link is an authorized go short link that must stay on the reposted page. It records the source before sending readers to the original article.

In the WordPress Media Library, when a file is uploaded, it first queries the database to check whether a file with the same filename exists; if it does, it continues to query the next filename

This causes the number of SQL requests to keep increasing after repeatedly uploading a file with the same filename (even if the file contents are different)

Many plugins currently either prepend a timestamp to the filename or directly rename the filename to a timestamp, which makes finding files very difficult. For this reason, I recommend getting the extension, adding a timestamp to the end of the filename, and then appending the extension back. Below is the complete code; you can test it freely in this temporary WordPress environment

If you do not yet have your own WordPress site, you can use a 雨云 server to quickly build one

Comments

Discussion

Be kind and keep the discussion friendly.

Complete your profile details to improve the chance of approval.

Your email will not be shown publicly.

Loading...

Markdown supported.

Discussion

0 comments

Internal link graph

Loading internal links…

View linked articles