I'm going through a WordPress plugin and restructuring the whole thing, fixed a few bugs, created a new folder structure, and added some shortcodes.
I want to roll out my updates as version 2. I'm wondering, does it matter that I renamed the plugin's main file using my custom prefix?
For example: I changed xmascount.php, to cw_xmascount.php.
Do I just delete all the files and folders of the original version of the plugin from the repository and then upload the new ones?
Very old question, but still relevant. And missing a real answer...
The short answer:
Changing the main plugin file name is not OK in the sense that WordPress depends on the plugin "folder/file-name" name combination to keep track of active plugins. This means that changing either one of those on an update (or via FTP or file manager) on a live site will cause WordPress to loose sight of the plugin.
The plugin will not be loaded anymore (read: "deactivate" without going through the normal deactivation process) and when you visit the WordPress admin Plugins page, you will see an admin error notice about the missing plugin.
But...
There is a way around this :)
The long answer:
The way plugin management (and updates) is set up in WordPress does not allow for a simple name change for the main plugin file. But even though WordPress has not foreseen in elegant solution for plugin developers to change the main plugin file name without breaking updates, the following approach should allow you to do it:
<?php
/**
* My Plugin old main file upgrade routine
*
* This is the old plugin main file.
* Keep it for backward upgrade compatibility.
*
* @package My Plugin
*/
defined( 'WPINC' ) || die;
$old = 'old-file-name.php'; // Change this.
$new = 'new-file-name.php'; // Change this.
// Change the active plugin settings to make WP start using the new one.
$active_plugins = (array) get_option( 'active_plugins', array() );
$old_plugin_array = array( basename( __DIR__ ) . '/' . $old );
$active_plugins = array_diff( $active_plugins, $old_plugin_array );
$new_plugin = basename( __DIR__ ) . '/' . $new;
if ( ! in_array( $new_plugin, $active_plugins ) ) {
$active_plugins[] = $new_plugin;
include_once __DIR__ . '/' . $new;
}
// Update active plugins and never come back here.
update_option( 'active_plugins', $active_plugins );
What this does:
When users upgrade the plugin, WordPress (unaware of the file name change) will first attempt to load the old file. Loading the file will run the code, which: gets the active plugins list, then removes the old file reference, adds the new file reference and saves the new active plugins list.
It will also load the new plugin file to make sure there is no "gap" in plugin availability.
The file should only serve once after the plugin upgrade. After that, it will not be used anymore. Nor will it be visible in the WP admin Plugins list.
Notes and caveats:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With