• rw1

    (@rw1)


    hello,

    when automatically upgrading a plugin recently i realised i lost some functionality as i had placed a folder containing files within the plugin directory.

    so it seems the auto upgrade had overwritten the contents of the plugin folder and removed the custom folder and its contents.

    is there a way to prevent a plugin auto upgrade from overwriting the contents of the plugin folder?

    i’ve found this post which seems to offer a solution but i wasn’t completely sure how to implement it, for example were the modifications to be added to functions.php? or is there a way to add code to the plugin update itself to prevent the overwrite occurring?

    Any help understanding the implementation of this solution or others much appreciated.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • f15d9f87d

    (@boyevul)

    Taken from the linked post:

    “In order to prevent WordPress plugin update from deleting our important folder within our plugin, we must move that particular folder somewhere safe before it starts deleting everything in our plugin. After the update has completed, we will then move the folder that we want to preserved and overwrite any existing files in that folder. We can’t remove the filter action as shown on the core WordPress code was because the filter was not added previously. Thus, removing the filter will not prevent the deletion.

    (bold for emphasis.)

    Thread Starter rw1

    (@rw1)

    Thanks very much for your reply. I understand the logic, but not the implementation at this stage.

    Would you be able to tell me where the second section of code goes? Is it in the functions.php file?

    I’ve also annotated the second section of code (see below) with my guess of what is occurring and what values could be replaced. Does this look about right?

    function customfolder_copyr($source, $dest) // <------- defines customfolder copy action
    {
        // Check for symlinks
        if (is_link($source)) {
            return symlink(readlink($source), $dest);
        }
    
        // Simple copy for a file
        if (is_file($source)) {
            return copy($source, $dest);
        }
    
        // Make destination directory
        if (!is_dir($dest)) {
            mkdir($dest);
        }
    
        // Loop through the folder
        $dir = dir($source);
        while (false !== $entry = $dir->read()) {
            // Skip pointers
            if ($entry == '.' || $entry == '..') {
                continue;
            }
    
            // Deep copy directories
            customfolder_copyr("$source/$entry", "$dest/$entry"); // <------- defines customfolder copy action
        }
    
        // Clean up
        $dir->close();
        return true;
    }
    function customfolder_backup()  // <------- customfolder backup
    {
        $to = dirname(__FILE__)."https://www.mysite-name.com/back-up-directory/"; // <------- this back up directory will be made
        $from = dirname(__FILE__)."https://www.mysite-name.com/wp-content/plugins/plugin_name/custom-folder/"; // <------- this is the directory that will be backed up
        customfolder_copyr($from, $to); // <------- executes customfolder copy action
    }
    function customfolder_recover() // <------- customfolder recover
    {
        $from = dirname(__FILE__)."https://www.mysite-name.com/back-up-directory/"; // <------- recover the files from this back up
        $to = dirname(__FILE__)."https://www.mysite-name.com/wp-content/plugins/plugin_name/custom-folder/"; // <------- to this location
        customfolder_copyr($from, $to); // <------- executes customfolder copy action
        if (is_dir($from)) {
            customfolder_rmdirr($from);#https://www.mysite-name.com/back-up-directory/ // <------- deletes the backup directory
        }
    }
    add_filter('upgrader_pre_install', 'customfolder_backup', 10, 2); / <------- adds the customfolder_backup filter
    add_filter('upgrader_post_install', 'customfolder_recover', 10, 2); <------- adds the customfolder_recover filter

    (initial code source)

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘how to prevent plugin folder being overwritten on plugin upgrade’ is closed to new replies.