Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to allow users to upload custom files to specific plugin directory. The problem is, obviously, that WP removes everything upon plugin version update.

I think that the upgrader_pre_install and upgrader_post_install hooks may be useful, but unfortunately so far I wasn't able to find any info to help me.

In your opinion, can these hooks be used to retain a directory and if so, how? Should I simply copy the directory elsewhere, like the uploads folder, and copy it back with upgrader_post_install? What would be the best practice?

Thanks

share|improve this question

2 Answers 2

up vote 1 down vote accepted

To answer my own question here is what I've found for the sake of keeping questions answered. What this does is copying the important directory before upgrade and recovering it after upgrade:

function my_dir_copy($source, $dest)
{
    if (is_link($source)) {
        return symlink(readlink($source), $dest);
    }
    if (is_file($source)) {
        return copy($source, $dest);
    }
    if (!is_dir($dest)) {
        mkdir($dest);
    }
    $dir = dir($source);
    while (false !== $entry = $dir->read()) {
        if ($entry == '.' || $entry == '..') {
            continue;
        }
        my_dir_copy("$source/$entry", "$dest/$entry");
    }

    $dir->close();
    return true;
}


function my_dir_rmdirr($dirname)
{

    if (!file_exists($dirname)) {
        return false;
    }


    if (is_file($dirname)) {
        return unlink($dirname);
    }


    $dir = dir($dirname);
    while (false !== $entry = $dir->read()) {

        if ($entry == ‘.’ || $entry == ‘..’) {
        continue;
        }
        rmdirr(“$dirname/$entry”);
    }


    $dir->close();
    return rmdir($dirname);
}


function my_dir_backup()
{
        $to = dirname(__FILE__)."/../igr_backup/";
        $from = dirname(__FILE__)."/whatever_directory_you_preserve/";
        my_dir_copy($from, $to);
}
function my_dir_recover()
{
        $from = dirname(__FILE__)."/../igr_backup/";
        $to = dirname(__FILE__)."/whatever_directory_you_preserve/";
        my_dir_copy($from, $to);
        if (is_dir($from)) {
                my_dir_rmdirr($from);
        }
}
add_filter('upgrader_pre_install', 'my_dir_backup', 10, 2);
add_filter('upgrader_post_install', 'my_dir_recover', 10, 2);
share|improve this answer
    
I'm accepting my own answer here since it works and answers my specific question. I have to say though for further notice that if you don't have multiple sub-directories in the preserved one, just keep your important extra files elsewhere in the first place, like @toscho suggested –  Daniel Sachs Sep 21 '12 at 12:32

The plugin directory cannot be used for uploads:

  1. You might not have write access here.
  2. It will be deleted on plugin updates as you just found out.
  3. Required CDN settings will probably fail for this directory.

Use the uploads directory for uploads. It is the only safe place.
See also: How to prevent specific plugin files or subfolders from appearing under “plugin files” listing with related problem – and the same solution.

share|improve this answer
    
Thanks Toscho, I'm aware your previous answer. What I actually had in mind was for users to upload their custom CSS files to that directory via FTP and then select those files for usage in the plugin options. For instance if the user has built his own custom theme for the plugin he or she will upload the theme files to designated directory and select it in the plugin option page. So in a sense I have no problem with access to that directory nor any CDN issues... –  Daniel Sachs Sep 16 '12 at 9:20
1  
FTP is dead. Make your users life easy and create a directory in the uploads directory or just treat the CSS files as regular attachments. In my setups you could not write to the plugin directory. And that problem would be your most annoying support question. –  toscho Sep 16 '12 at 9:29
    
hmm... You're probably right, but what happens when the theme contains js files as well as css, divided into child directories? I think I have to use ftp for this, don't I? –  Daniel Sachs Sep 17 '12 at 9:28
    
What happens if multiple installation share one plugin directory? If there are too many files for your plugin create a ZIP file the user can install as a new theme. I would say: If you need subdirectories your plugin does too much … but that’s hard to judge with so little information. :) –  toscho Sep 17 '12 at 10:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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