Prevent WordPress Plugin Update From Deleting Important Folder In Your Plugin

Many would know that i have build a powerful thumbnail plugin in WordPress. The plugin work perfectly fine for WordPress 2.8 below but once i updated my WordPress to version 2.8, something bad happen. Every single time whenever i update my WordPress plugin, WordPress will delete the plugin folder with everything included and install the latest plugin version. This post a problem to my plugin since all the images are stored inside the plugin folder. I search online but i couldn’t get any help anywhere until i investigate the new version 2.8 core codes.

New Hooks in WordPress 2.8

In WordPress 2.8, it has added a new functionality to delete the previous version of the plugin and then install the latest plugin version into the exact same location. Lucky, it also includes a few more filter hook that did the delete and install process. In class-wp-upgrade.php located at /admin/includes/class-wp-upgrade.php, lines 403-409 you will notice the following few codes,

                add_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade'), 10, 2);
                add_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'), 10, 4);
                //'source_selection' => array(&$this, 'source_selection'), //theres a track ticket to move up the directory for zip's which are made a bit differently, useful for non-.org plugins.

                $this->run(array(
                                        'package' => $r->package,
                                        'destination' => WP_PLUGIN_DIR,
                                        'clear_destination' => true,
                                        'clear_working' => true,
                                        'hook_extra' => array(
                                                                'plugin' => $plugin
                                        )
                                ));

                //Cleanup our hooks, incase something else does a upgrade on this connection.
                remove_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade'));
                remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'));

I went to search on Google again for these new hook and it seems like there are a couple more of these new hooks being added into WordPress 2.8. But we are particularly interested on ‘upgrader_pre_install‘ and ‘upgrader_post_install‘ filter hook.

Using upgrade_pre_install filter hook

function hpt_copyr($source, $dest)
{
    // 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
        hpt_copyr("$source/$entry", "$dest/$entry");
    }

    // Clean up
    $dir->close();
    return true;
}
function hpt_backup()
{
        $to = dirname(__FILE__)."/../hpt_images_backup/";
        $from = dirname(__FILE__)."/images/";
        hpt_copyr($from, $to);
}
function hpt_recover()
{
        $from = dirname(__FILE__)."/../hpt_images_backup/";
        $to = dirname(__FILE__)."/images/";
        hpt_copyr($from, $to);
        if (is_dir($from)) {
                hpt_rmdirr($from);#http://putraworks.wordpress.com/2006/02/27/php-delete-a-file-or-a-folder-and-its-contents/
        }
}
add_filter('upgrader_pre_install', 'hpt_backup', 10, 2);
add_filter('upgrader_post_install', 'hpt_recover', 10, 2);

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.

 

Like this post? Share it!

digg 48 Prevent Wordpress Plugin Update From Deleting Important Folder In Your Plugin reddit 48 Prevent Wordpress Plugin Update From Deleting Important Folder In Your Plugin stumbleupon 48 Prevent Wordpress Plugin Update From Deleting Important Folder In Your Plugin delicious 48 Prevent Wordpress Plugin Update From Deleting Important Folder In Your Plugin furl 48 Prevent Wordpress Plugin Update From Deleting Important Folder In Your Plugin technorati 48 Prevent Wordpress Plugin Update From Deleting Important Folder In Your Plugin google 48 Prevent Wordpress Plugin Update From Deleting Important Folder In Your Plugin myspace 48 Prevent Wordpress Plugin Update From Deleting Important Folder In Your Plugin facebook 48 Prevent Wordpress Plugin Update From Deleting Important Folder In Your Plugin twitter 48 Prevent Wordpress Plugin Update From Deleting Important Folder In Your Plugin
share save 171 16 Prevent Wordpress Plugin Update From Deleting Important Folder In Your Plugin

No related posts.

About Clay

I am Clay who is the main writer for this website. I own a small web hosting company in Malaysia and i'm available to be hired as individual contractor on elance. You can find me on twitter.
This entry was posted in How-to, Open Source and tagged . Bookmark the permalink.

21 Responses to Prevent WordPress Plugin Update From Deleting Important Folder In Your Plugin

  1. Pingback: Prevent Wordpress Plugin Update From Deleting Important Folder In …

  2. Pingback: Prevent Wordpress Plugin Update From Deleting Important Folder In … « Meanings of Words

  3. Pingback: Prevent Wordpress Plugin Update From Deleting Important Folder In … « Meanings of Words

  4. Pingback: How to Prevent Basement Water Damage? « Eco 1st Technology Group

  5. Pingback: Buy SEO Services in India and Add Wings to Your Business | PimpSEO - The King of SEO

  6. Pingback: The reasons why you should opt for SEO | PimpSEO - The King of SEO

  7. Pingback: The ABC Of Article Promotion And How To Improve Sales On Your Site | PimpSEO - The King of SEO

  8. Pingback: Affairs at the Jupiter Hotel » Color Scheming For Designers

  9. Pingback: SEO Terminology: Boilerplate (How Google May be Treating Repeated Content) (Search Engine Journal) | PimpSEO - The King of SEO

  10. Pingback: Interesting Reads in Google News | PimpSEO - The King of SEO

  11. Pingback: Miss Philippines 2009 - Fail? | PimpSEO - The King of SEO

  12. Pingback: Job Interview (2009-08-24 10:49) | PimpSEO - The King of SEO

  13. Pingback: All about Seo Elite User Comments | PimpSEO - The King of SEO

  14. Pingback: Drea on Rise to the Top | PimpSEO - The King of SEO

  15. Pingback: Link Building Tweaked By Real Experts | PimpSEO - The King of SEO

  16. Pingback: The Best Financial Freedom Business to Join on the Internet | PimpSEO - The King of SEO

  17. Pingback: Article Writers Needed for Skin Care by xkcdhirer | PimpSEO - The King of SEO

  18. Pingback: Paid Signups needed daily - pay $5 per Signup by monclerc | PimpSEO - The King of SEO

  19. Pingback: Nonpublic project #495722 by Goldy30 | PimpSEO - The King of SEO

  20. Pingback: Design Rules To Live By #8 | This Is Aarons Life - Affairs at the Jupiter Hotel

  21. Pingback: The Most Creative 10 Websites And Apps Built On Top Of Twitter | Inspired Magazine - Affairs at the Jupiter Hotel