WP All Import:将图片导入帖子专属目录(如房产列表文件夹)
MLS列表同步WordPress的媒体目录专属化实现方案
1. 自动创建帖子专属目录(基于MLS编号)
利用WordPress的wp_upload_dir钩子,结合帖子的MLS自定义字段(假设字段名为mls_id),动态修改上传路径并自动创建不存在的目录。
代码示例:
add_filter('wp_upload_dir', 'custom_mls_upload_dir'); function custom_mls_upload_dir($param) { global $post; // 仅在处理房源类帖子的媒体上传时生效 if (!empty($post) && get_post_type($post) === 'your_property_post_type') { // 替换为你的房源自定义文章类型 $mls_id = get_post_meta($post->ID, 'mls_id', true); if (!empty($mls_id)) { // 构建MLS专属目录路径 $custom_dir = '/mls-listings/' . sanitize_file_name($mls_id); $param['path'] = $param['basedir'] . $custom_dir; $param['url'] = $param['baseurl'] . $custom_dir; $param['subdir'] = $custom_dir; // 自动创建目录(如果不存在) if (!file_exists($param['path'])) { wp_mkdir_p($param['path']); } } } return $param; }
注意:替换your_property_post_type为实际的房源自定义文章类型名,mls_id为存储MLS编号的自定义字段名。
2. 让WP All Import上传的图片存入专属目录
WP All Import默认调用WordPress原生上传函数,上述wp_upload_dir钩子已能覆盖大部分场景。若批量导入时未关联当前帖子,需借助WP All Import的钩子传递MLS编号:
add_action('pmxi_saved_post', 'save_mls_id_for_upload', 10, 3); function save_mls_id_for_upload($post_id, $xml_node, $import_id) { // 从导入数据中提取MLS编号(替换为你的XML字段路径) $mls_id = $xml_node->mls_id; if (!empty($mls_id)) { update_post_meta($post_id, 'mls_id', sanitize_text_field($mls_id)); // 临时存储MLS ID到全局变量,供上传钩子调用 global $current_import_mls_id; $current_import_mls_id = $mls_id; } } // 修正WP All Import的上传路径 add_filter('wp_all_import_upload_file', 'custom_mls_import_upload_path', 10, 3); function custom_mls_import_upload_path($path, $url, $attachment_id) { global $current_import_mls_id; if (!empty($current_import_mls_id)) { $upload_dir = wp_upload_dir(); $custom_dir = $upload_dir['basedir'] . '/mls-listings/' . sanitize_file_name($current_import_mls_id); wp_mkdir_p($custom_dir); // 拼接最终文件路径 $filename = basename($path); return $custom_dir . '/' . $filename; } return $path; }
3. 确保图片缩放/优化工具识别目录路径
多数图片优化工具依赖WordPress原生媒体元数据,主图片存入专属目录后,缩略图、优化文件会自动跟随路径。若部分工具存在识别问题,可通过钩子强制修正元数据路径:
add_filter('wp_generate_attachment_metadata', 'custom_mls_attachment_metadata', 10, 2); function custom_mls_attachment_metadata($metadata, $attachment_id) { $post_parent = wp_get_post_parent_id($attachment_id); if (!empty($post_parent)) { $mls_id = get_post_meta($post_parent, 'mls_id', true); if (!empty($mls_id)) { $custom_subdir = '/mls-listings/' . sanitize_file_name($mls_id); // 修正所有缩略图的存储路径 foreach ($metadata['sizes'] as $size => $size_data) { $metadata['sizes'][$size]['file'] = $custom_subdir . '/' . basename($size_data['file']); } } } return $metadata; }
额外优化:批量删除过期列表的媒体
目录专属化后,删除过期帖子时可直接删除对应MLS目录,无需逐个查找媒体文件:
add_action('before_delete_post', 'delete_mls_post_media_dir', 10, 1); function delete_mls_post_media_dir($post_id) { if (get_post_type($post_id) === 'your_property_post_type') { $mls_id = get_post_meta($post_id, 'mls_id', true); if (!empty($mls_id)) { $upload_dir = wp_upload_dir(); $custom_dir = $upload_dir['basedir'] . '/mls-listings/' . sanitize_file_name($mls_id); // 删除目录及所有子文件/子目录 if (file_exists($custom_dir)) { $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($custom_dir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST ); foreach ($files as $fileinfo) { $action = $fileinfo->isDir() ? 'rmdir' : 'unlink'; $action($fileinfo->getRealPath()); } rmdir($custom_dir); } } } }
内容的提问来源于stack exchange,提问作者Torn Marketing
相关产品推荐
相关产品推荐

