0

So I would like to allow the user to customize the css for my plugin, but if a user creates a css file and then places it in my plugin directory, the file gets deleted the next time plugin is updated because its not part of the plugin files.

How do I flag a user added file or sub-dir/file in my plugin folder so it does not get deleted?

Thanks

  • You can't store files in the plugin directory. You don't even know if you have write access there. Use the uploads directory only, you can create custom directories there. – fuxia Mar 20 at 5:07
  • @fuxia ok great that will solve my problem. You should leave an answer instead of comment so I can accept it. – jsherk Mar 20 at 5:12
3

Always use the upload directory to store files, nothing else. There are two reasons:

  1. Plugin directories are replaced on plugin updates, so your custom files will be deleted.
  2. The upload directory is the only one where you can expect write access. For example, I'm updating my plugins via composer, and there is no write access to that directory before and after that task.

Here is a simple illustration how that might work:

$uploads = wp_get_upload_dir();

if ( $uploads['error'] ) {
    // something is very wrong, stop messing here
}
else {
    $my_dirname = 'your_plugin_name';
    $full_path  = $uploads['basedir') . "/$my_dirname";

    if ( ! is_dir( $full_path ) {
        if ( ! mkdir( $full_path ) ) {
            // handle the case when you can't create a directory
        }           
    }
}

Now your upload directory path is $uploads['basedir') . "/$my_dirname", and your URL is $uploads['baseurl') . "/$my_dirname".

See the wp_get_upload_dir() documentation.

|improve this answer|||||

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.