Skip to content

Instantly share code, notes, and snippets.

@samstoller
Last active October 18, 2018 06:32
Show Gist options
  • Save samstoller/efc38eb309e62df392ea to your computer and use it in GitHub Desktop.
Save samstoller/efc38eb309e62df392ea to your computer and use it in GitHub Desktop.
How to use wp-cli with a non-standard wp-config.php location

You are running a customized WordPress install where your configurations are NOT located in one of the two required locations (outside the webroot for example) and are receving the following error when running wp-cli commands:

Error: Strange wp-config.php file: wp-settings.php is not loaded directly.

###Solution Simply move the require_once ... wp-settings.php line from the actual configuration file to the end of file that is requiring/including it. See below for examples.

This works by tricking the wp-cli regex into correctly parsing the configuration files.

├── conf
│   └── the-real-conf.php   <----|
├── public                       |
│   ├── index.php                |
│   └──  wp-config.php      -----|  (required file)
└── wp-cli.yml
<?php
// ... other code here... //
/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
// ... remove lines 13 + 14 ... //
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
<?php
require dirname(__DIR__) . '/conf/the-real-conf.php';
// ... place them AFTER the call to the real conf file ... //
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
@szepeviktor
Copy link

There is a WP core feature: wp-config.php could be just above WP root.
If you additionally move wp-content to a sibling dir you could checkout WP core from GitHub/WordPress.

├── public
│   ├── wp-config.php
│   ├── index.php  (added: "/core")
│   ├── core
│   │   └── index.php 
│   └── content
└── wp-cli.yml

wp-config.php in document root is secure.

@tennox
Copy link

tennox commented Oct 18, 2016

wp-config.php in document root is secure.

Why would it be secure? It is still inside the public webroot. This can lead to severe leaks - explained here

And @samstoller: Thank you for this useful gist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment