I have a plugin which will create a folder inside wp-content/uploads, using this code:
//Generate stuff on plugin activation
function sample_plugin_activate() {
$upload_dir = wp_upload_dir();
$files = array(
array(
'base' => $upload_dir['basedir'] . '/sample_folder',
'file' => 'index.html',
'content' => ''
)
);
foreach ( $files as $file ) {
if ( wp_mkdir_p( $file['base'] ) && ! file_exists( trailingslashit( $file['base'] ) . $file['file'] ) ) {
if ( $file_handle = @fopen( trailingslashit( $file['base'] ) . $file['file'], 'w' ) ) {
fwrite( $file_handle, $file['content'] );
fclose( $file_handle );
}
}
}
}
register_activation_hook( __FILE__, 'sample_plugin_activate' );
Later on in my script i generate XML files inside this folder:
//Save XML
$UploadDir = wp_upload_dir();
$UploadURL = $UploadDir['basedir'];
$location = realpath($UploadURL . "/sample_folder/");
$xmlfile = $location.'/'.$orderId.'.xml';
$test = file_put_contents($xmlfile, $xml);
This works fine. My problem is that i'm running a multisite setup. The sample_folder is generated inside wp-content/uploads/sample_folder, but once i'm on a multisite, wp_upload_dir() will return the upload folder like this: wp-content/uploads/sites/12/ and obviously theres no sample_folder inside this path, which will cause errors in my plugin.
How can i fix this?