I was once asked a question:

Is it possible to emulate a request in WP-CLI, so that all of the various globals are correctly populated when I start poking in wp shell?

The answer is “yes.”

  1. First, you need to specify the request URL when invoking wp shell. For example, wp shell --url=https://wildwolf.name/how-to-log-request-headers-with-nginx-and-njs/
  2. Then, inside the shell, you need to invoke the wp() function. It will parse the request and set all variables.
  3. If you need to access a global variable, you need to run global $variable_name; first.
Sample wp shell session
Sample wp shell session
How to Emulate a Request in WP CLI Shell
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *