wp-cli: wp doesn't fully populate $_SERVER array

UPDATE The actual issue is that $_SERVER is not being fully set up when wp-cli is run. If you put

error_log($_SERVER[‘SERVER_NAME’]);

into wp-config.php and run any wp command, you’ll get a blank line. For my setup, this sets WP_CONTENT_URL and WP_CONTENT_DIR to invalid results, and the command fails.

— original report

According to the docs the taxonomy is supplied as the first option and the term is the second. When I do this using a custom taxonomy, the command fails.

Example: typetax is a properly declared and working custom taxonomy using (the full declaration is omitted for clarity)

register_taxonomy( 'typetax', 'stories', $args );

but when this is run

wp term create typetax 'Banana'

I get an error: Error: Invalid taxonomy

If I run

wp term create post_tag 'Banana'

it works. So the issue seems to be that WP-CLI doesn’t understand Custom Taxonomies.

The CT is declared via an mu-plugin FTR.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16 (4 by maintainers)

Most upvoted comments

EDIT: Using a switch wasn’t working for wp-admin URLs because the basename was outputting wp-admin. Using if and strstr to check if the cwd had a string in it works for both wp-admin and the live site. It feels clunky but it’s an option.

In case anyone’s looking for a workaround: getcwd() will get the current path and works in php cli php4+. Note that when accessing wp-admin it includes wp-admin because these requests aren’t handled by the index.php in the wordpress root. That’s why I’m using strstr to check if my path exists in the string.

Depending on your environment directory naming scheme this can work for multiple wp-config.php settings.

if(strstr(getcwd(),‘/full/path/to/wp/dev’)) { // DEV code / settings } elseif(strstr(getcwd(),‘/full/path/to/wp/stage’)) { // STAGE // STAGE code / settings } else { // PRODUCTION // PRODUCTION code / settings }

wp-config.php in case it’s relevant


// Note that $_SERVER['DOCUMENT_ROOT'] will return a string with a trailing slash on
//  some systems and not others, so we'll make sure it's gone in all cases where there
//  is ambiguity by using rtrim.
// Also note that this is above the loading of the wp-config-*.php files so that these can
//  be over-ridden as required.

$md_http = ($_SERVER['HTTPS']) ? 'https://': 'http://';
$md_server_name = rtrim($_SERVER['SERVER_NAME'],"/");
$md_document_root = rtrim($_SERVER['DOCUMENT_ROOT'],"/");

// The wordpress install is in its own folder, so we force that setting here
define('WP_SITEURL', $md_http . $md_server_name . '/wordpress');
define('WP_HOME',    $md_http . $md_server_name);


// The wordpress wp-content folder is outside of the git WordPress submodule so
//     tell WP where it is. These may have to be set manually in some cases
define('WP_CONTENT_URL', $md_http . $md_server_name . '/wp-content');
define('WP_CONTENT_DIR', $md_document_root . '/wp-content');


// Four stages
// local = local development on my workstation
// staging = site is on my staging server
// live = site is live and under my ongoing maintenance
// released = site is live and I play no role in maintenance. In this case, the logic 
//  block below is removed and everything reverts back to a single file, and all traces
//  of git are purged leaving a normal site. This is all done manually btw.

if ( file_exists( dirname( __FILE__ ) . '/wp-config-local.php' ) ) {
    define( 'MD_LOCAL_DEV', true );
    include( dirname( __FILE__ ) . '/wp-config-local.php' );

    // I don't need cron running, so turn it off
    define( 'DISABLE_WP_CRON', true );

} else if ( file_exists( dirname( __FILE__ ) . '/wp-config-staging.php' ) ) {
    define( 'MD_STAGING_DEV', true );
    include( dirname( __FILE__ ) . '/wp-config-staging.php' );

    // Disable all core updates, as they are handled on local machine and done via git
    define( 'WP_AUTO_UPDATE_CORE', false );
    define( 'DISALLOW_FILE_MODS', true );
    define( 'AUTOMATIC_UPDATER_DISABLED', true );

    // I Handle cron via system cron, so disable wp_cron
    define( 'DISABLE_WP_CRON', true );

} else if ( file_exists( dirname( __FILE__ ) . '/wp-config-live.php' ) ) {
    define( 'MD_LIVE', true );
    include( dirname( __FILE__ ) . '/wp-config-live.php' );

    //Disable all core updates, as they are handled on local machine and done via git
    define( 'WP_AUTO_UPDATE_CORE', false );
    define( 'DISALLOW_FILE_MODS', true );
    define( 'AUTOMATIC_UPDATER_DISABLED', true );

    // Disable all debugging
    define('WP_DEBUG', false);
}


// NOTE: If upgrading from an old installation and DB_CHARSET and DB_COLLATE do not exist 
//  in the wp-config.php file, DO NOT ADD THEM. Read more here
//  https://codex.wordpress.org/Editing_wp-config.php#Database_character_set
// Database Charset to use in creating database tables.
define('DB_CHARSET', 'utf8');

// The Database Collate type. Don't change this if in doubt.
define('DB_COLLATE', '');

/**
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
// NOTE: This is a placeholder replaced with new salts via the site creation script
//  It will throw an error if not done correctly. This is by design.

define('AUTH_KEY',         '+/l:+c+^-RimCPsX7D]|[ G7>Yq2+lP T2[7/If2p-.${)((v-c<ubi*<8QOF.Q_');
define('SECURE_AUTH_KEY',  'FKgfL}M)DW6I5iQ-8pqF-op{6{o&d4KNHWxA|~jdR&kXO<(%AD`*i|RCp>S1>XDq');
define('LOGGED_IN_KEY',    ')|adOIXS>U(x 0UOV^.LhebLn_GUk,^x.I-0~R6<ig;q|e2X%ri`mG4S%cO2NYE|');
define('NONCE_KEY',        'G;tydGEsOy1`Wg9d%x{_+j~Z7@DI}^)fq$fNTu!82oVU!5S|y+lS[s~()&0fz&]-');
define('AUTH_SALT',        'Z1TvD2qp|ZJF.:uS3#P]%U/J&|ktGQe}HA@NQVXBFW?GM{?n.!h:aK!o-]hpU 5G');
define('SECURE_AUTH_SALT', ' 3NK2z#TK,X1FIv6#4C0iEQl$}ebQ{|-IHEm.wo;6/Fsp;8$xmb:]K?m,>6-`z.-');
define('LOGGED_IN_SALT',   'o+oy|<p/*m+>w}?n8!V^ybzDq*q-NiJ5NuQ0%FF/-I3w?zspN T!3C&b`_!YIADI');
define('NONCE_SALT',       '6lhg~>o:FBfm|j H<Nb++tqg$zyjp--?VWs$XAOa+T3{aN}dZq)4FBzo^54n)xz*');

/**
 * WordPress Localized Language, defaults to English.
 *
 * Change this to localize WordPress. A corresponding MO file for the chosen
 * language must be installed to wp-content/languages. For example, install
 * de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
 * language support.
 */
define('WPLANG', '');

/* That's all, stop editing! Happy blogging. */

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');