How do I run a PHP script from WordPress environment, like `wp shell`?

wp shell is a very useful tool that lets you run PHP statements and expressions interactively, from within a WordPress environment. It lets you use all the functions, classes and globals that you can use within WordPress.

Is there a similar tool that will let me run a PHP file?

1
1

Yes, there is! I didn’t find it at first, but wp cli offers a command called eval-file which does what you want:

$ echo '<?php echo is_wp_error("foo") ? 1 : 0;' > example.php
$ wp eval-file example.php
0

Leave a Comment