Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the acf domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u378631216/domains/wphai.com/public_html/wp-includes/functions.php on line 6131

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the easy-watermark domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u378631216/domains/wphai.com/public_html/wp-includes/functions.php on line 6131

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the shortcodes-ultimate domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u378631216/domains/wphai.com/public_html/wp-includes/functions.php on line 6131

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the limit-login-attempts-reloaded domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u378631216/domains/wphai.com/public_html/wp-includes/functions.php on line 6131
如何在WordPress中实现上传文件自动重命名? - WP大海

要在WordPress上传文件自动重命名,可以使用插件如”Media File Renamer”或”File Renaming on Upload”。安装并激活这些插件后,按照插件的设置选项配置重命名规则,即可在上传文件自动重命名

WordPress中上传文件时,自动重命名功能对于提升网站SEO友好性和避免文件名重复或乱码具有重要作用,小编将详细介绍几种实现自动重命名的方法,并提供相应的代码示例:

1、使用时间戳重命名

原理解析:通过获取文件上传时的时间戳作为文件名,确保每个文件的名称都是独一无二的,这种方法简单有效,特别适合于需要快速处理大量上传文件的场景。

实现方法

可以将下面的代码添加到当前主题的functions.php文件中。

function rename_uploads($filename) {
     $info = pathinfo($filename);
     $ext = empty($info[‘extension’]) ? ” : ‘.’ . $info[‘extension’];
     $name = current_time(‘mysql’).$ext;
     return $name;
}
add_filter(‘sanitize_file_name’, ‘rename_uploads’, 10);

效果展示:文件将被命名为“年月日时分秒+千位毫秒整数”的格式,如2bcd.com20221011122221765.jpg

2、MD5加密重命名

原理解析:MD5可以生成32位的字符串,将原始文件名通过MD5加密后作为新文件名,这也能有效避免文件名重复。

实现方法:同样地,将代码添加至functions.php文件中。

function rename_to_md5($filename) {
    $info = pathinfo($filename);
    $ext = empty($info[‘extension’]) ? ” : ‘.’ . $info[‘extension’];
    $name = md5(uniqid()).$ext;
    return $name;
}
add_filter(‘sanitize_file_name’, ‘rename_to_md5’, 10);

效果展示:文件名将变为MD5加密后的32位字符串,例如6dcd4ce23d88e2ee95838f7b014b6284.jpg

3、自定义字符串重命名

原理解析:除了使用程序生成的随机字符串,也可以自定义特定的字符串格式作为文件名前缀或后缀,以便更好地管理文件。

实现方法:修改functions.php文件,根据需要自定义命名规则。

function custom_rename($filename) {
    $info = pathinfo($filename);
    $ext = empty($info[‘extension’]) ? ” : ‘.’ . $info[‘extension’];
    $name = ‘customPrefix_’ . date(‘YmdHis’) . $ext;
    return $name;
}
add_filter(‘sanitize_file_name’, ‘custom_rename’, 10);

效果展示:文件名可能类似于customPrefix_20221011122221765.jpg

在了解以上内容后,以下还有一些其他建议:

确保所修改的functions.php已备份,防止编码错误导致网站功能异常。

考虑到未来可能更换主题,尽量使用子主题或插件来添加这些功能,以保持功能的持久性。

自动重命名功能可能会影响之前根据原文件名设置的链接或内容,需要检查确保无断链或错误引用。

考虑到图片优化,自动重命名后的图片URLs可能需要重新生成,使用SEO友好的URL结构。

在WordPress中实现上传文件自动重命名是一个提高网站维护效率和SEO表现的有效手段,通过上述方法,可以根据时间戳、MD5加密或自定义规则来重命名上传的文件,这不仅方便了文件管理,也优化了网站的搜索引擎排名,在实施过程中,注意做好文件备份和兼容性测试,确保网站平稳运行。

 

常见问题
如何下载?
可以点击右侧边栏或者文章底部的【立即下载】按钮。然后按照【网盘地址】进行下载。
不是最新版本?
可以提交工单。或者发邮件到 uishop@qq.com。
声明:1、本站大部分资源均为网络采集所得,仅供用来学习研究,请于下载后的24h内自行删除,正式商用请购买正版。2、所有汉化类文件和个别标注了“原创”的产品均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。3、如若本站内容侵犯了原著者的合法权益,请携带相关版权文件联系我们进行下架或删除。4、虚拟下载类资源具有可复制性,一经下载后本站有权拒绝退款或更换其他商品!
0

评论0

没有账号?注册  忘记密码?

社交账号快速登录