Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions references/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,52 @@ Here's an annotated example `wp-cli.yml` file:
# Inherit configuration from an arbitrary YAML file
inherit: prod.yml

### HTTP proxy configuration

WordPress does not automatically use proxy environment variables such as
`http_proxy` for its HTTP API. For WordPress HTTP API requests during a WP-CLI
run, you can load a small PHP file that defines the same proxy constants
WordPress supports in `wp-config.php`:

```yaml
# wp-cli.yml
require:
- proxy.php
```

```php
<?php
// proxy.php

$proxy = getenv( 'HTTP_PROXY' ) ?: getenv( 'http_proxy' );

if ( ! $proxy ) {
return;
}

$proxy_url = parse_url( $proxy );

if ( is_array( $proxy_url ) ) {
if ( ! empty( $proxy_url['host'] ) ) {
define( 'WP_PROXY_HOST', $proxy_url['host'] );
}

if ( ! empty( $proxy_url['port'] ) ) {
define( 'WP_PROXY_PORT', $proxy_url['port'] );
}
}

$no_proxy = getenv( 'NO_PROXY' ) ?: getenv( 'no_proxy' );

if ( $no_proxy ) {
define( 'WP_PROXY_BYPASS_HOSTS', $no_proxy );
}
Comment on lines +344 to +360
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This snippet has a potential compatibility issue with PHP 8.0+. If parse_url() returns false (which can happen with malformed URLs), attempting to access $proxy_url['host'] will throw a TypeError. Additionally, including support for no_proxy (via WP_PROXY_BYPASS_HOSTS) is highly recommended, as it prevents local requests (like those to the database or the local site itself) from being routed through the proxy, which is a common cause of failure in WP-CLI environments.

Suggested change
$proxy_url = parse_url( $proxy );
if ( ! empty( $proxy_url['host'] ) ) {
define( 'WP_PROXY_HOST', $proxy_url['host'] );
}
if ( ! empty( $proxy_url['port'] ) ) {
define( 'WP_PROXY_PORT', $proxy_url['port'] );
}
$proxy_url = parse_url( $proxy );
if ( is_array( $proxy_url ) ) {
if ( ! empty( $proxy_url['host'] ) ) {
define( 'WP_PROXY_HOST', $proxy_url['host'] );
}
if ( ! empty( $proxy_url['port'] ) ) {
define( 'WP_PROXY_PORT', $proxy_url['port'] );
}
}
$no_proxy = getenv( 'NO_PROXY' ) ?: getenv( 'no_proxy' );
if ( $no_proxy ) {
define( 'WP_PROXY_BYPASS_HOSTS', $no_proxy );
}

```

This configures the proxy for HTTP requests made through WordPress core during a
WP-CLI run. HTTP requests made before WordPress has loaded may need separate
handling.

## Remote (SSH) configuration

Using the `ssh` option, WP-CLI can be configured to run on a remote system rather than the current system. Along with the SSH protocol, WP-CLI also supports connecting to Docker containers (including docker-compose) and Vagrant VMs.
Expand Down