修改 WordPress 上传的子目录,让其不同的文件类型存放在不同的子目录
<?php
/*
Plugin Name: Custom Upload by File Type
Description: 根据文件类型上传到 UPLOADS 常量定义的子目录并生成正确 URL
Version: 1.2
Author: Your Name
*/

// 定义上传根目录常量
define('UPLOADS', '你的上传目录');

// 在上传前获取扩展名并设置子目录
function my_upload_prefilter($file) {
    $file_ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
    global $my_upload_subdir;

    // 根据扩展名分组
    switch ($file_ext) {
        case 'jpg':
        case 'jpeg':
        case 'png':
        case 'gif':
            $my_upload_subdir = '/images';
            break;
        case 'pdf':
        case 'doc':
        case 'docx':
            $my_upload_subdir = '/documents';
            break;
        case 'mp4':
        case 'avi':
        case 'mov':
            $my_upload_subdir = '/videos';
            break;
        case 'zip':
        case '7z':
        case 'rar':
            $my_upload_subdir = '/archives';
            break;
        default:
            $my_upload_subdir = '/others';
            break;
    }

    return $file;
}
add_filter('wp_handle_upload_prefilter', 'my_upload_prefilter');

// 自定义上传目录
function custom_upload_media_dir($uploads) {
    global $my_upload_subdir;
    if ($my_upload_subdir) {
        // 设置物理路径
        $uploads['basedir'] = UPLOADS;
        $uploads['path'] = UPLOADS . $my_upload_subdir;
        // 设置 URL
        $uploads['baseurl'] = home_url();
        $uploads['url'] = $uploads['baseurl'] . $my_upload_subdir;
        $uploads['subdir'] = $my_upload_subdir;

        // 确保目录存在
        if (!file_exists($uploads['path'])) {
            wp_mkdir_p($uploads['path']);
        }
    }
    return $uploads;
}
add_filter('upload_dir', 'custom_upload_media_dir');

// 处理上传并确保文件路径正确
function my_handle_upload($upload) {
    global $my_upload_subdir;
    if ($my_upload_subdir) {
        $filename = basename($upload['file']);
        $new_path = UPLOADS . $my_upload_subdir . '/' . $filename;

        // 如果文件不在目标路径,移动它
        if ($upload['file'] !== $new_path) {
            rename($upload['file'], $new_path);
            $upload['file'] = $new_path;
        }

        // 更新 URL
        $upload['url'] = home_url($my_upload_subdir . '/' . $filename);
    }
    return $upload;
}
add_filter('wp_handle_upload', 'my_handle_upload', 10, 1);

// 修正附件 URL
function my_attachment_url($url, $post_id) {
    $file = get_attached_file($post_id);
    if ($file) {
        $file_ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
        $filename = basename($file);

        // 根据扩展名确定子目录
        switch ($file_ext) {
            case 'jpg':
            case 'jpeg':
            case 'png':
            case 'gif':
                $subdir = '/images';
                break;
            case 'pdf':
            case 'doc':
            case 'docx':
                $subdir = '/documents';
                break;
            case 'mp4':
            case 'avi':
            case 'mov':
                $subdir = '/videos';
                break;
            case 'zip':
            case '7z':
            case 'rar':
                $subdir = '/archives';
                break;
            default:
                $subdir = '/others';
                break;
        }

        return home_url($subdir . '/' . $filename);
    }
    return $url;
}
add_filter('wp_get_attachment_url', 'my_attachment_url', 10, 2);
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇