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.

Need a website? Let's talk.

From website design & SEO through to custom WordPress plugin development. I transform ideas into dynamic, engaging, and high-performing solutions.
Subscribe to get the latest insights & updates in the world of web and how it impacts your business and website.
© 2024 Phil Kurth  |  All rights reserved.