-
Notifications
You must be signed in to change notification settings - Fork 27
/
wp_all_import_images_uploads_dir.php
71 lines (56 loc) · 2.25 KB
/
wp_all_import_images_uploads_dir.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* =========================================
* Filter: wp_all_import_images_uploads_dir
* =========================================
*
* Can be used to set a custom path in which images (as well as media intended for ACF fields) are uploaded.
* Only applies to media uploaded via WP All Import.
*
* @since 4.4.9
*
* @param $uploads array - Contains information related to the WordPress uploads path & URL
* @param $articleData array - Contains a list of data related to the post/user/taxonomy being imported
* @param $current_xml_node array - Contains a list of nodes within the current import record
* @param $import_id int - Contains the ID of the import
*
* @return string
*/
add_filter('wp_all_import_images_uploads_dir', 'wpai_wp_all_import_images_uploads_dir', 10, 4);
function wpai_wp_all_import_images_uploads_dir($uploads, $articleData, $current_xml_node, $import_id){
return $uploads;
}
// ----------------------------
// Example uses below
// ----------------------------
/**
* Upload all images for the post to a folder called "customfolder"
*
*/
function wpai_set_custom_upload_folder($uploads, $articleData, $current_xml_node, $import_id) {
$uploads['path'] = $uploads['basedir'] . '/customfolder';
$uploads['url'] = $uploads['baseurl'] . '/customfolder';
if (!file_exists($uploads['path'])) {
mkdir($uploads['path'], 0755, true);
}
return $uploads;
}
add_filter('wp_all_import_images_uploads_dir', 'wpai_set_custom_upload_folder', 10, 4);
/**
* Upload all images for the post to a folder based on the post date (in Y/m format)
*
* (e.g. if the post was published on June 1st 2017, its images would
* be uploaded to /wp-content/uploads/2017/06)
*
*/
function wpai_set_upload_folder_by_post_date($uploads, $articleData, $current_xml_node, $import_id) {
if ( ! empty($articleData['post_date'])) {
$uploads['path'] = $uploads['basedir'] . '/' . date("Y/m", strtotime($articleData['post_date']));
$uploads['url'] = $uploads['baseurl'] . '/' . date("Y/m", strtotime($articleData['post_date']));
if (!file_exists($uploads['path'])) {
mkdir($uploads['path'], 0755, true);
}
}
return $uploads;
}
add_filter('wp_all_import_images_uploads_dir', 'wpai_set_upload_folder_by_post_date', 10, 4);