2

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

2 Answers 2

1

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);
1
  • 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 Commented Sep 21, 2012 at 12:32
0

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.

4
  • 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... Commented Sep 16, 2012 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.
    – fuxia
    Commented Sep 16, 2012 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? Commented Sep 17, 2012 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. :)
    – fuxia
    Commented Sep 17, 2012 at 10:42

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.