Use WP CLI to tinker with your codebase via an interactive shell

If you have access to WP CLI I can’t recommend it enough. Since I picked it up it has saved me so much time during development. It helps me scaffold code, analyse the installation, update core and plugins, and it even helps me test code on the fly.

Testing code on the fly with wp shell

The wp shell command is frickin’ sweet as, bro!

You pump it into your command line and voila, you are in an interactive shell with your WordPress environment all loaded up. From here you can invoke any functions loaded up by WordPress, plugins, or your theme, not to mention all of PHP’s built-in functionality. You can even assign variables, write functions, and run expressions you are used to writing in PHP.

A demo of the wp shell command for WP CLI

It is a fantastic way to test and check your code and I often find myself using it to test whether a function is going to do what I need or if some combination of, say, array functions will result in what I’m thinking they will.

Just one thing to keep in mind…

If you change your code in any way, you need to exit and then run wp shell again as your code changes won’t be picked up until you do.

One last thing…

If you liked this article, I'd be incredibly grateful if you tweeted about it.

Also, I don't send emails often but when I do, I try to fill them full of useful goodies. If you like code snippets & dev tips, join my mailing list. There's no catch but I'll probably want to tell you about any new plugins & tools I build.