Site upload path incorrect
Not sure if this is just a 'me' issue, but when I was uploading media from a networked site outside of the media_site the upload month/year path was incorrect. It still worked, but the file was being uploaded into the incorrect month/year directories.
Hi @forkbombe, can you provide some more information on what behaviour you're seeing here? i.e. what's the expected vs actual path?
Is the subdirectory using an incorrect date, or not including the date at all? If the latter, note that this is dependent on the uploads_use_yearmonth_folders option, which is stored per-site (Options > Media > Organize my uploads into month- and year-based folders) - this is WordPress' behaviour, not this plugin's.
Of course! Apologies for the lack of detail. It also may very well be something else causing this issue.
The setting in WordPress to define year/month paths is on, this is labelled as uploads_use_yearmonth_folders in the site settings.
When I use this network-media-library code, it does give a year/month path - however, the year/month part is incorrect (see below for example 2017/07):
Now, I seem to be able to get around this problem by switch_back_to_media_site() and ensuring that upload path / data is set to whatever that site outputs in wp_get_upload_dir() - but I am not aware of any potential knock-on issues this might cause.
function filter_upload_dir($uploads) {
// Static variable to prevent recursive calls
static $in_filter = false;
// If already in the filter, return the original uploads array
if ($in_filter) {
return $uploads;
}
// Set the static variable to true to indicate we're in the filter
$in_filter = true;
// If not on the media site, switch to it
if (!is_media_site()) {
switch_to_media_site();
$media_uploads = wp_get_upload_dir();
restore_current_blog();
// If media uploads directory is valid, update the uploads array
if ($media_uploads && !is_wp_error($media_uploads)) {
$uploads['subdir'] = $media_uploads['subdir'];
$uploads['path'] = $media_uploads['path'];
$uploads['url'] = $media_uploads['url'];
$uploads['basedir'] = $media_uploads['basedir'];
$uploads['baseurl'] = $media_uploads['baseurl'];
}
}
// Reset the static variable to false
$in_filter = false;
// Return the modified uploads array
return $uploads;
}
// Add the filter to modify the upload directory
add_filter('upload_dir', __NAMESPACE__ . '\filter_upload_dir');
That is very strange behaviour indeed. Core uses current_time( 'mysql' ) to get the date/month; is it possible your server's time is set incorrectly?
current_time('mysql') outputs correctly in the environment I'm currently working in.
I seem to have this issue on my local machine and both staging and production servers.
If no-one else has created an issue for it then perhaps it's safe to assume it's a 'me' problem. I think I have a safe fix for my case anyway.
Thanks!