3

I am trying to automate the updating of private custom plugins. I found this question: Updates for a private plugin?, which works brilliantly. I have the zip files on a website and WordPress detects the upgrade to the plugin and the user can then ask for the update.

The problem I am having is that GitHub adds the version onto the plugin directory, once it is upzipped so:

  • myplugin -> myplugin-1.9

Because of this feature of GitHub, WordPress thinks it is a new plugin and the user has to re-activate it after the update.

UPDATE

I have since watched the code run in WP_Upgrader and there doesn't seem to be a hook or filter in which I can change the final directory for the plugin.

I did add some code to the filter upgrader_post_install to rename the directory, which worked, but too hacky for a final solution.

I do not see a moment where I can hop in and ask the directory be changed from myplugin-1.9 to myplugin. I have reached the conclusion for now, that I will instead build the zip file myself to ensure it does not have the version number.

1 Answer 1

1

It should be possible to carefully adjust the destination path via the upgrader_package_options filter in WordPress, before the upgrader installs the new version of it.

Here's a barebone example that appends the plugin's current directory name to the destination path, if it's same as WP_PLUGIN_DIR (see check in core here), when the package url is a github.com domain:

add_filter( 'upgrader_package_options', function( $options ) { $destination = $options['destination'] ?? ''; $package = $options['package'] ?? ''; $dirname = isset( $options['hook_extra']['plugin'] ) ? dirname( $options['hook_extra']['plugin'] ) : ''; if ( empty( $dirname ) || '.' === $dirname ) { return $options; } if ( 'github.com' !== parse_url( $package, PHP_URL_HOST) ) { return $options; } if ( WP_PLUGIN_DIR === $destination ) { $options['destination'] = path_join( $destination, $dirname ); } return $options; }); 
4
  • Thanks! I ended up debugging and watching WP_Upgrader class unpack the plugin and $destination holds only the plugin directory. I will be updating my question, because I cannot find a hook or filter to use after stepping through the code. Commented May 22, 2023 at 13:33
  • @Katie Thanks for the info. I've updated the snippet. Commented May 22, 2023 at 17:47
  • 1
    Thank you for the solution. So it seems the $options['destination'] normally has just the /plugins directory in it, but when you add /plugins/plugin-name directory to it, then it forces the directory name of the plugin? Commented May 22, 2023 at 19:26
  • 1
    Yes that is how I understand it and the WP_PLUGIN_DIR comparison should also target only plugins and not themes. Commented May 22, 2023 at 19:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.