WP_CLI

Runner::load_wordpress()publicWP-CLI 1.0

Load WordPress, if it hasn't already been loaded

Method of the class: Runner{}

No Hooks.

Return

null. Nothing (null).

Usage

$Runner = new Runner();
$Runner->load_wordpress();

Runner::load_wordpress() code WP-CLI 2.8.0-alpha

public function load_wordpress() {
	static $wp_cli_is_loaded;
	// Globals not explicitly globalized in WordPress
	global $site_id, $wpdb, $public, $current_site, $current_blog, $path, $shortcode_tags;

	if ( ! empty( $wp_cli_is_loaded ) ) {
		return;
	}

	$wp_cli_is_loaded = true;

	// Handle --context flag.
	$this->context_manager->switch_context( $this->config );

	WP_CLI::debug( 'Begin WordPress load', 'bootstrap' );
	WP_CLI::do_hook( 'before_wp_load' );

	$this->check_wp_version();

	$wp_config_path = Utils\locate_wp_config();
	if ( ! $wp_config_path ) {
		WP_CLI::error(
			"'wp-config.php' not found.\n" .
			'Either create one manually or use `wp config create`.'
		);
	}

	WP_CLI::debug( 'wp-config.php path: ' . $wp_config_path, 'bootstrap' );
	WP_CLI::do_hook( 'before_wp_config_load' );

	// Load wp-config.php code, in the global scope
	$wp_cli_original_defined_vars = get_defined_vars();

	eval( $this->get_wp_config_code() ); // phpcs:ignore Squiz.PHP.Eval.Discouraged

	foreach ( get_defined_vars() as $key => $var ) {
		if ( array_key_exists( $key, $wp_cli_original_defined_vars ) || 'wp_cli_original_defined_vars' === $key ) {
			continue;
		}

		// phpcs:ignore PHPCompatibility.Variables.ForbiddenGlobalVariableVariable.NonBareVariableFound
		global ${$key};
		// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		${$key} = $var;
	}

	$this->maybe_update_url_from_domain_constant();
	WP_CLI::do_hook( 'after_wp_config_load' );
	$this->do_early_invoke( 'after_wp_config_load' );

	// Prevent error notice from wp_guess_url() when core isn't installed
	if ( $this->cmd_starts_with( [ 'core', 'is-installed' ] )
		&& ! defined( 'COOKIEHASH' ) ) {
		define( 'COOKIEHASH', md5( 'wp-cli' ) );
	}

	// Load WP-CLI utilities
	require WP_CLI_ROOT . '/php/utils-wp.php';

	// Set up WordPress bootstrap actions and filters
	$this->setup_bootstrap_hooks();

	// Load Core, mu-plugins, plugins, themes etc.
	if ( Utils\wp_version_compare( '4.6-alpha-37575', '>=' ) ) {
		if ( $this->cmd_starts_with( [ 'help' ] ) ) {
			// Hack: define `WP_DEBUG` and `WP_DEBUG_DISPLAY` to get `wpdb::bail()` to `wp_die()`.
			if ( ! defined( 'WP_DEBUG' ) ) {
				define( 'WP_DEBUG', true );
			}
			if ( ! defined( 'WP_DEBUG_DISPLAY' ) ) {
				define( 'WP_DEBUG_DISPLAY', true );
			}
		}
		require ABSPATH . 'wp-settings.php';
	} else {
		require WP_CLI_ROOT . '/php/wp-settings-cli.php';
	}

	// Fix memory limit. See https://core.trac.wordpress.org/ticket/14889
	// phpcs:ignore WordPress.PHP.IniSet.memory_limit_Blacklisted -- This is perfectly fine for CLI usage.
	ini_set( 'memory_limit', -1 );

	// Load all the admin APIs, for convenience
	require ABSPATH . 'wp-admin/includes/admin.php';

	add_filter(
		'filesystem_method',
		static function () {
			return 'direct';
		},
		99
	);

	// Re-enable PHP error reporting to stderr if testing.
	if ( getenv( 'BEHAT_RUN' ) ) {
		$this->enable_error_reporting();
	}

	WP_CLI::debug( 'Loaded WordPress', 'bootstrap' );
	WP_CLI::do_hook( 'after_wp_load' );

}