子比主题美化 附件自定义添加前缀名

源码介绍

在附件的文件名前加一个自定义的前缀,方便很多资源站或是个人博客下载网站,可以备注网名等醒目的文字标注区分等,下载类的文章这个功能很实用,需要的自取。

子比主题美化 附件自定义添加前缀名-大海资源库
子比主题美化 附件自定义添加前缀名-大海资源库

使用教程

/wp-content/themes/zibll/inc/options/admin-options.php

子比主题美化 附件自定义添加前缀名-大海资源库
//新增自定义文案文件名前缀开关
                'custom' => __('自定义文案', 'zib_language'),
                //新增自定义文案文件名前缀开关end
            ),
                //新增自定义文案文件名前缀
                'desc'       => __('选择上传文件重命名的格式', 'zib_language'),
            ),
            array(
                'dependency' => array('newfilename_type', '==', 'custom'),
                'title'      => __('自定义文案', 'zib_language'),
                'subtitle'   => __('输入自定义的文案作为文件名前缀', 'zib_language'),
                'id'         => 'newfilename_custom_text',
                'type'       => 'text',
                'default'    => '',
            ),
            //新增自定义文案文件名前缀end

/wp-content/themes/zibll/inc/functions/zib-attachment.php

这段整体替换

子比主题美化 附件自定义添加前缀名-大海资源库
function zib_new_filename($file)
{
    $newfilename_type = _pz('newfilename_type');
    if ($newfilename_type === 'random') {
        $info         = pathinfo($file['name']);
        $ext          = empty($info['extension']) ? '' : '.' . $info['extension'];
        $md5          = md5($file['name']);
        $file['name'] = substr($md5, 0, 10) . current_time('YmdHis') . $ext;
    } elseif ($newfilename_type === 'time') {
        $file['name'] = current_time('YmdHis') . mt_rand(10, 99) . mt_rand(0, 9) . '-' . $file['name'];
    } elseif ($newfilename_type === 'custom') {
        $custom_text  = _pz('newfilename_custom_text');
        $file['name'] = $custom_text . '-' . $file['name'];
    }

    return $file;
}

if (_pz('newfilename')) {
    add_filter('wp_handle_upload_prefilter', 'zib_new_filename', 99);
    add_filter('wp_handle_sideload_prefilter', 'zib_new_filename', 99);
}
温馨提示: 本文最后更新于2024-08-24 21:24:43,若有错误或已失效,请在下方 留言或联系 站长
有问题及时联系站长,QQ:1240555208
更多优质资源在QQ群里,可以进群领取:467392290~
© 版权声明
THE END
点赞5 分享
及时反馈~ 抢沙发

请登录后发表评论

    暂无评论内容