惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
Jina AI
Jina AI
Google DeepMind News
Google DeepMind News
大猫的无限游戏
大猫的无限游戏
The GitHub Blog
The GitHub Blog
腾讯CDC
L
LangChain Blog
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
量子位
Apple Machine Learning Research
Apple Machine Learning Research
Cloudbric
Cloudbric
B
Blog RSS Feed
B
Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
S
Schneier on Security
Project Zero
Project Zero
宝玉的分享
宝玉的分享
美团技术团队
MyScale Blog
MyScale Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy
T
Threatpost
A
Arctic Wolf
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
爱范儿
爱范儿
Recorded Future
Recorded Future
C
CERT Recently Published Vulnerability Notes
T
Threat Research - Cisco Blogs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
SecWiki News
SecWiki News
H
Hacker News: Front Page
AWS News Blog
AWS News Blog
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tor Project blog
C
Check Point Blog
L
Lohrmann on Cybersecurity
F
Full Disclosure
TaoSecurity Blog
TaoSecurity Blog
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
The Last Watchdog
The Last Watchdog
Martin Fowler
Martin Fowler

模板兔

WordPress模板开发之hook钩子media_view_strings的使用教程 WordPress插件开发之manage_comments_custom_column的使用方法 WordPress主题定制之hook钩子media_upload_tabs的用法详解 WordPress插件开发之lostpassword_user_data钩子的使用教程 WordPress模板开发之manage_media_columns钩子的用法详解 WordPress主题开发之manage_media_custom_column钩子的使用教程 WordPress模板开发之hook钩子pre_get_col_charset的用法 - 模板兔 WordPress主题开发之pre_months_dropdown_query的用法说明 - 模板兔 WordPress主题开发之hook钩子pre_load_script_translations的使用教程 - 模板兔 WordPress插件开发之hook钩子network_plugin_loaded的使用说明 - 模板兔 WordPress插件开发之pre_render_block的使用教程 - 模板兔 WordPress hook钩子pre_oembed_result的用法详解 - 模板兔 WordPress主题定制之hook钩子post_locked_dialog的用法详解 - 模板兔 WordPress插件开发之hook钩子pre_cache_alloptions的用法详解 - 模板兔 WordPress模板定制开发之previous_comments_link_attributes的调用方法 - 模板兔 WordPress主题开发之hook钩子post_comments_feed_link_html的用法介绍 - 模板兔 WordPress模板开发之hook钩子posts_clauses的使用教程解析 - 模板兔 WordPress插件开发之hook钩子mejs_settings的用法详解 - 模板兔 wordpress获取指定多个分类ID以及其子分类_WordPress教程 - 模板兔 WordPress主题定制之post_playlist钩子的使用教程 - 模板兔 WordPress模板开发之hook钩子post_embed_url的用法详解 - 模板兔 WordPress模板开发之preprocess_comment钩子的用法介绍 - 模板兔 WordPress插件开发之hook钩子populate_site_meta的用法详解 - 模板兔 WordPress主题开发之posts_request_ids钩子的用法详解 - 模板兔 WordPress模板定制开发之pre_category_nicename的用法介绍 - 模板兔
wordpress前台某处上传图片到媒体库单独放进一个文件夹_WordPress教程 - 模板兔
模板兔 · 2026-06-08 · via 模板兔

最近模板兔给某个客户二次开发时,需要对前台工单里上传的图片单独放入一个ticket文件夹且也走媒体库逻辑。比如默认上传到媒体库是wp-content/upload/2026/06/目录,我这个前台上传工单图片到wp-content/upload/ticket/2026/06/目录。

前台原始上传代码:

if ( ! function_exists( 'wp_handle_upload' ) ) { require_once( ABSPATH . 'wp-admin/includes/file.php' ); } $upload = wp_handle_upload($_FILES['imageFile'], array('test_form' => false)); if ($upload && !isset($upload['error'])) { $attachment = array( 'guid' => $upload['url'], 'post_mime_type' => $upload['type'], 'post_title' => preg_replace('/\.[^.]+$/', '', basename($upload['file'])), 'post_content' => '', 'post_status' => 'inherit' ); $attachment_id = wp_insert_attachment($attachment, $upload['file']); if (!is_wp_error($attachment_id)) { require_once( ABSPATH . 'wp-admin/includes/image.php' ); // 生成元数据并更新附件 $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload['file']); wp_update_attachment_metadata($attachment_id, $attachment_data); $image = wp_get_attachment_url($attachment_id); $error = 0; } }

修改后的代码:

if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}

add_filter('upload_dir', function($upload) {
$custom_subdir = '/ticket' . $upload['subdir'];
$upload['path'] = $upload['basedir'] . $custom_subdir;
$upload['url'] = $upload['baseurl'] . $custom_subdir;
$upload['subdir'] = $custom_subdir;
return $upload;
}, 99);

$upload = wp_handle_upload($_FILES['file'], array('test_form' => false));

// 上传完立即移除过滤器,不影响其他代码
remove_filter('upload_dir', 99);

if ($upload && !isset($upload['error'])) {
$attachment = array(
'guid' => $upload['url'],
'post_mime_type' => $upload['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($upload['file'])),
'post_content' => '',
'post_status' => 'inherit'
);

$attachment_id = wp_insert_attachment($attachment, $upload['file']);

if (!is_wp_error($attachment_id)) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );

// 生成元数据并更新附件
$attachment_data = wp_generate_attachment_metadata($attachment_id, $upload['file']);
wp_update_attachment_metadata($attachment_id, $attachment_data);

$image = wp_get_attachment_url($attachment_id);

$error = 0;
}
}