I read in another question that all the files in the directory are deleted during an update and this is bad design. Got it. Prevent plugin from deleting important directories
Is it possible to not modify one of my plugin’s files during an update? I have a text file in the plugin directory into which the user has entered data.
The reason my users have edited one of the files is twofold:
- I don’t like admin pages for simple plugins
- The plugin editor makes it easy for users to make text file edits
Can I get the file contents before the file is deleted? Can I preserve that file in any way with an update by checking it’s modified date and copying it before it dies?
Store the text in an option, not in a file. Two advantages:
wp-config.php
containsdefine( 'DISALLOW_FILE_EDIT',true );
.Never try to write in the plugin and theme directories.
What you can do is in the plugin files that you distribute have a text-sample.txt which on init (or some other hook that executes shortly after you update the plugin) checks to see if text.txt file exists and if not copy text-sample.txt to text.txt. WordPress does something similar with wp-config.php. The php command to do this is:
As @toscho mentioned though, it would be better to have it in the wp_options table if possible (IMO it is much easier and safer for a user to edit either through an admin page or through wp-admin/options.php that it is for them to upload a text file).