handbook/guides/sharing-wp-cli-packages.md
Pascal Birchler 264cb131bf
Fix handbook information architecture
- Organize files in folders according to the desired structure
- Use directory iterator to loop through all files and folders (makes generation more robust)
2025-06-25 15:38:39 +02:00

38 lines
2.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Sharing WP-CLI Packages
By default, WP-CLI places [installed packages](https://developer.wordpress.org/cli/commands/package/) in `~/.wp-cli/packages/`, a hidden subdirectory for the users home directory.
Because the home directory is different for each user, this naturally means each system user will have a separate directory of installed packages. If you have multiple active shell users on a server, and want to share installed WP-CLI packages between them, there are a couple of supported ways to do this.
## `WP_CLI_PACKAGES_DIR` environment variable
To override the directory WP-CLI uses for installed packages, provide a `WP_CLI_PACKAGES_DIR` environment variable. If you wish for multiple users to share the same packages directory, you can simply provide the same `WP_CLI_PACKAGES_DIR` environment variable for each user.
vim /etc/environment
export WP_CLI_PACKAGES_DIR=/usr/local/lib/wp-cli-packages
Similarly, you can make sure the directory is only writable by a specific user to make packages available to all users, but only installable by the specific user.
Its worth noting the `WP_CLI_PACKAGES_DIR` environment variable *overrides* WP-CLIs default behavior of loading packages installed in the users home directory. If you want to support both, youll need to take the second approach.
### Composer project in a shared directory
WP-CLIs installed packages directory is simply a Composer project under the hood. Given this architecture, you can create your own Composer project in an arbitrary directory, and load it into scope using WP-CLIs `--require=<path>` flag.
First, create your Composer project.
$ mkdir /usr/local/lib/wp-cli-packages
$ cd /usr/local/lib/wp-cli-packages
$ composer init -n --name=runcommand/wp-cli-packages -s=dev --repository=https://wp-cli.org/package-index/
$ composer require runcommand/hook
Using version dev-master for runcommand/hook
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing runcommand/hook (dev-master 7a7beae)
Cloning 7a7beae2013eeea243cc44524a7c5c21da11979e
Writing lock file
Generating autoload files
Now, once your Composer project has a dependency or two, you can use `wp --require=<path/to/autoload>` (or the equivalent `config.yml` statement) to load the packages into WP-CLI.