0

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?

0

It seems like you are creating the folder on main site and trying to read from it on a sub site. This is not the right way to go, remeber that on multisite each site, except for user management, is almost 100% isolated full wordpress that has no easy access to data from other sites.

Assuming there should be only one such file in the system, What you probably need to do is to do the file creation and access in the context of the main site which is usually blog #1, something like

// to write
swicth_to_blog(1);
write file data
restore_blog();

// to read
swicth_to_blog(1);
read file data
restore_blog();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.