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.
No related posts.












Pingback: Prevent Wordpress Plugin Update From Deleting Important Folder In …
Pingback: Prevent Wordpress Plugin Update From Deleting Important Folder In … « Meanings of Words
Pingback: Prevent Wordpress Plugin Update From Deleting Important Folder In … « Meanings of Words
Pingback: How to Prevent Basement Water Damage? « Eco 1st Technology Group
Pingback: Buy SEO Services in India and Add Wings to Your Business | PimpSEO - The King of SEO
Pingback: The reasons why you should opt for SEO | PimpSEO - The King of SEO
Pingback: The ABC Of Article Promotion And How To Improve Sales On Your Site | PimpSEO - The King of SEO
Pingback: Affairs at the Jupiter Hotel » Color Scheming For Designers
Pingback: SEO Terminology: Boilerplate (How Google May be Treating Repeated Content) (Search Engine Journal) | PimpSEO - The King of SEO
Pingback: Interesting Reads in Google News | PimpSEO - The King of SEO
Pingback: Miss Philippines 2009 - Fail? | PimpSEO - The King of SEO
Pingback: Job Interview (2009-08-24 10:49) | PimpSEO - The King of SEO
Pingback: All about Seo Elite User Comments | PimpSEO - The King of SEO
Pingback: Drea on Rise to the Top | PimpSEO - The King of SEO
Pingback: Link Building Tweaked By Real Experts | PimpSEO - The King of SEO
Pingback: The Best Financial Freedom Business to Join on the Internet | PimpSEO - The King of SEO
Pingback: Article Writers Needed for Skin Care by xkcdhirer | PimpSEO - The King of SEO
Pingback: Paid Signups needed daily - pay $5 per Signup by monclerc | PimpSEO - The King of SEO
Pingback: Nonpublic project #495722 by Goldy30 | PimpSEO - The King of SEO
Pingback: Design Rules To Live By #8 | This Is Aarons Life - Affairs at the Jupiter Hotel
Pingback: The Most Creative 10 Websites And Apps Built On Top Of Twitter | Inspired Magazine - Affairs at the Jupiter Hotel