wp shell

If you’re a WordPress backend developer and don’t use wp shell, you must continue reading.

I have this imaginary plugin that is used to fetch some data from a third-party service and store every item as a post of a custom post type. Now I need to figure out how something deep down in this very complex integration works, but there’s a problem: To be able to test it locally I’d have to start an import that will fetch GB’s of data that I don’t need. Only to simulate what happens when a specific function is being executed as part of that import. wp shell to the rescue!

Let’s say that there’s an \ImaginaryPlugin\format_description( string $description ) function that is the function we’d like to test or debug. There’s also an ImaginaryPlugin\get_item( int $item_id ) function that can be used to get an item. Testing it on an existing item is as simple as this:

$ wp shell

wp> $item = \ImaginaryPlugin\get_item( 123 );
  array(3) {
  'id' =>
  int(123)
  'name' =>
  string(7) "Test"
  'description' =>
  string(14) "This_is_a_test"
}

wp> \ImaginaryPlugin\format_description( $item['description'] );
string(14) "This is a test"

Awesome! Being able to do this can make debugging or testing a breeze compared to actually testing things by refreshing the web page or running new imports over and over again.

There are of course other ways to test this specific and rather bad example, but the point of this post still stands: wp shell is very useful!

In general, the whole WP CLI project is an addition to WordPress that I find extremely useful. If you’re not familiar with it, check it out: https://wp-cli.org/

Receive a weekly update in your inbox

I'd very much appreciate it if you'd be interested in getting an email delivered directly to your inbox every Sunday morning at 9AM with a summary of my tech related blog posts from the last week. I will not in any way use your email address for anything else. That's a promise.

Leave a Comment