admin管理员组

文章数量:1390650

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

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

Share Improve this question asked Mar 20, 2020 at 5:03 jsherkjsherk 1873 silver badges15 bronze badges 2
  • 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 Commented Mar 20, 2020 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 Commented Mar 20, 2020 at 5:12
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: How to let user store a file in plugin directory but not have it get deleted on update