1

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?

2
  • Loved that @toscho provided exactly two counter-reasons :) You don't need to create a plugin page, use one that already exists with the Settings API.
    – brasofilo
    Commented Apr 9, 2013 at 4:30
  • Too late. I'd rather let the plugin go stale than deal with all the angry users.
    – rg89
    Commented Apr 9, 2013 at 14:05

2 Answers 2

3

Store the text in an option, not in a file. Two advantages:

  1. The option will not be deleted during the update.
  2. Your users can edit it even when their wp-config.php contains define( 'DISALLOW_FILE_EDIT',true );.

Never try to write in the plugin and theme directories.

4
  • I really appreciate your effort in this forum, but you didn't answer my question. I even included a link to a question you answered to preempt your "best advice" response. It didn't work!
    – rg89
    Commented Apr 9, 2013 at 4:32
  • Please explain what exactly didn’t work. The simple answer to your question is no – to short for a real answer here. :)
    – fuxia
    Commented Apr 9, 2013 at 4:35
  • I knew you were going to answer this with "use an option," so I tried to prevent that from happening. Sure, if I start a new plugin today, an option is probably a better idea, but it's too late for that in the context of this question.
    – rg89
    Commented Apr 9, 2013 at 14:10
  • @rg89 The problem is: you cannot update your plugin now and keep the file content. It is lost before you implement your new code.
    – fuxia
    Commented Apr 9, 2013 at 14:14
1

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:

if (!file_exists($destination) {
    copy($source, $destination);
}

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).

4
  • This an interesting idea, but by the time my new init code runs, the file will have been deleted and replaced with the new version.
    – rg89
    Commented Apr 9, 2013 at 14:02
  • That is not correct. Let me emphasise that with the plugin files that you distribute, you do not give them a file with the same name as the file you use (for example wordpress never ships with a wp-config.php file, but always ships with a wp-config-sample.php which it will then edit and copy to wp-config.php if required). Commented Apr 9, 2013 at 20:56
  • Are you suggesting to rename every file that makes up my plugin? It is my understanding that the plugin directory is wiped during an update, and there is no way to distribute an update and preserve the contents of the old files.
    – rg89
    Commented Apr 15, 2013 at 14:09
  • Not at all - only the text file (which should have been stored as a wordpress option as mentioned by others). If the way you are updating your plugins wipes the existing plugin directory, then what I have suggested will not work. What I have suggested will work if you upload the plugin via ftp (I haven't looked into what happens if you use the wordpress admin interface). Commented Apr 15, 2013 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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