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
1 Answer
Reset to default 3Always use the upload directory to store files, nothing else. There are two reasons:
- Plugin directories are replaced on plugin updates, so your custom files will be deleted.
- 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
版权声明:本文标题:How to let user store a file in plugin directory but not have it get deleted on update? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744655055a2617914.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论