Skip to content

wp-env has no Internet access #70265

@bor0

Description

@bor0

Propose to add something like the follows in README.md to make this more explicit.


Internet access

By default, wp-env does not have access to the Internet. You can get around this by adding the following to wp-config.php:

defined( 'WP_PROXY_HOST' ) or define( 'WP_PROXY_HOST', 'socks://host.docker.internal' );
defined( 'WP_PROXY_PORT' ) or define( 'WP_PROXY_PORT', '8080' );

A shortcut command to achieve that:

$ wp-env run cli -- sh -c 'echo -e "\ndefined( '\''WP_PROXY_HOST'\'' ) or define( '\''WP_PROXY_HOST'\'', '\''socks://host.docker.internal'\'' );\ndefined( '\''WP_PROXY_PORT'\'' ) or define( '\''WP_PROXY_PORT'\'', '\''8080'\'' );" | tee -a wp-config.php'

Example scenario

Install wp-env:

$ npm -g i @wordpress/env
...
$ wp-env start
...

Try making a request to the Internet (only WP.org works):

$ wp-env run cli wp shell
ℹ Starting 'wp shell' on the cli container. Exit the WordPress shell with ctrl-c.

wp> wp_remote_retrieve_body( wp_remote_get( 'https://woocommerce.com/wp-json/helper/1.0/oauth/authorize' ) )
=> string(0) ""
wp> wp_remote_retrieve_body( wp_remote_get( 'https://wordpress.org' ) )
=> string(144732) "<!DOCTYPE html>...

Add the following lines to wp-config.php:

defined( 'WP_PROXY_HOST' ) or define( 'WP_PROXY_HOST', 'socks://host.docker.internal' );
defined( 'WP_PROXY_PORT' ) or define( 'WP_PROXY_PORT', '8080' );
$ wp-env run cli -- sh -c 'echo -e "\ndefined( '\''WP_PROXY_HOST'\'' ) or define( '\''WP_PROXY_HOST'\'', '\''socks://host.docker.internal'\'' );\ndefined( '\''WP_PROXY_PORT'\'' ) or define( '\''WP_PROXY_PORT'\'', '\''8080'\'' );" | tee -a wp-config.php'

$ wp-env run cli cat wp-config.php | tail -n 2
ℹ Starting 'cat wp-config.php' on the cli container. 

✔ Ran `cat wp-config.php` in 'cli'. (in 0s 645ms)
defined( 'WP_PROXY_HOST' ) or define( 'WP_PROXY_HOST', 'socks://host.docker.internal' );
defined( 'WP_PROXY_PORT' ) or define( 'WP_PROXY_PORT', '8080' );

Now, try making a request to the Internet again:

$ wp-env run cli wp shell
ℹ Starting 'wp shell' on the cli container. Exit the WordPress shell with ctrl-c.
wp> wp_remote_retrieve_body( wp_remote_get( 'https://woocommerce.com/wp-json    /helper/1.0/oauth/authorize' ) )
=> string(191963) "
<!DOCTYPE html>
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions