How WP-CLI loads WordPress

Shelly Bashful loves the command line. Since she’s been using it daily at work, she knows all the little tricks that make typing at the console a breeze.

She’d been put in charge of a WordPress site a few weeks ago and was sorely missing a CLICLI Command Line Interface. Terminal (Bash) in Mac, Command Prompt in Windows, or WP-CLI for WordPress. management tool. All modern web frameworks have one and Drupal has Drush. For cron jobs, she could trigger HTTPHTTP HTTP is an acronym for Hyper Text Transfer Protocol. HTTP is the underlying protocol used by the World Wide Web and this protocol defines how messages are formatted and transmitted, and what actions Web servers and browsers should take in response to various commands. requests, but that seemed like a really roundabout way of doing things.

She figures that if she manages to load the WP environment from a CLI script, the rest would be easy. After a bit of trial-and-error, she comes up with a few lines of codeLines of Code Lines of code. This is sometimes used as a poor metric for developer productivity, but can also have other uses.:

define('WP_ADMIN', true);

Shelly is pleased; her script even works on multisiteMultisite Multisite is a WordPress feature which allows users to create a network of sites on a single WordPress installation. Available since WordPress version 3.0, Multisite is a continuation of WPMU or WordPress Multiuser project. WordPress MultiUser project was discontinued and its features were included into WordPress core. installs. Both the host name and the path to the WordPress install are hardcoded, but she can figure ways around that later.

With her geeky curiosity satisfied, she lazily types in a google search, to check if anyone else has solved this problem. The first result is a project called WP-CLIWP-CLI WP-CLI is the Command Line Interface for WordPress, used to do administrative and development tasks in a programmatic way. The project page is She installs it and tries out a few commands – it seems to work pretty well.

She then goes to the WP-CLI source and searches for ‘wp-load.php’, to see how it handles the WordPress bootstrapping – 0 results. Puzzled, she starts going through the first file that gets executed and eventually reaches one called wp-setting-cli.php. This strikes Shelly as peculiar, since she encountered a similar file in WordPress CoreCore Core is the set of software required to run WordPress. The Core Development Team builds WordPress.. She signs into the project’s IRC channel.

shelly: “Why does WP-CLI use a modified copy of the wp-settings.php file?”

After a while, someone responds:

scribu: “Because WordPress does several things that don’t make sense in a CLI context. For example, if the database tables are not installed, it prints out an HTMLHTML HTML is an acronym for Hyper Text Markup Language. It is a markup language that is used in the development of web pages and websites. page with a link to the setup screen.”

shelly: “Ok, but won’t that break when WordPress adds or removes files?”

scribu: “Yes, it would, but keeping our copy up-to-date hasn’t been too difficult so far and the control it gives us is worth it. In particular, it would be tricky to avoid loading the advanced-cache.php drop-in otherwise.”

shelly: “Wouldn’t the caching problem be solved by setting WP_ADMIN to true?”

scribu: “We tried that, but it turns out there are a lot of plugins that attempt to blockBlock Block is the abstract term used to describe units of markup that, composed together, form the content or layout of a webpage using the WordPress editor. The idea combines concepts of what in the past may have achieved with shortcodes, custom HTML, and embed discovery into a single consistent API and user experience. wp-admin access, by calling wp_redirect() and then exit(). So now we load the admin-specific code explicitly.”

shelly: “Fair enough.”