wp eval-file

Loads and executes a PHP file.

Note: because code is executed within a method, global variables need to be explicitly globalized.

See also wp eval


wp eval-file {file} [{arg}...] [--skip-wordpress]

You can specify global options and the following:

The path to the PHP file to execute. Use '-' to run code from STDIN.
One or more arguments to pass to the file. They are placed in the $args variable.
Load and execute file without loading WordPress.

Source code of the commands


#1 Demonstration

Run the work.php file in the site root, pass param1 and param2 parameters and disable WordPress environment:

wp eval-file work.php param1 param2 --skip-wordpress

work.php file content:

print_r( $args );
/* show in the console
	[0] => param1
	[1] => param2

print_r( $_SERVER['argv'] );
/* show in the console
	[0] => D:\server72\modules\wp-cli\wp-cli.phar
	[1] => eval-file
	[2] => work.php
	[3] => param1
	[4] => param2
	[5] => --skip-wordpress

#2 Run several CLI commands from the file

The code is useful when transferring a site from a local (test) server to a production one. Import the database backup (db.sql file) and do the replacement of the old domain to the new one.

wp eval-file work.php --skip-wordpress

work.php file content:

$commands = [ 
	'wp db import db.sql',
	'wp search-replace old-domain.com new-domain.com'

foreach ( $commands as $command ) {
	echo "\n$command\n" . shell_exec( $command );