7

I've installed WP-CLI on the Mac and my next step is to execute WP-CLI commands using PHP script.

I've tried to implement it the following way but I do not see anything happening. Can someone please look at my code and tell me that what I'm doing wrong?

I'm using Docker and have WP installed in Docker/xamp/www/wordpress_wwws/htdocs directory

Do I need to execute the PHP code from within the WP directory or can It be in any directory on the server?

define( 'WP_CLI_ROOT', '/usr/local/bin/wp' );
include WP_CLI_ROOT . '';
$output = shell_exec("wp --info");
echo "<pre>".$output."</pre>";

Do I need to configure and setup wp-cli with my PHP files?

Also, when I type wp --info on my terminal the following information comes up. Nothing is appearing beside the Package Dir & global config. do I also need to make adjustments to wp-cli?

MAC-00343:htdocs mike$ wp --info
PHP binary: /usr/bin/php
PHP version:    5.6.30
php.ini used:   
WP-CLI root dir:    phar://wp-cli.phar
WP-CLI vendor dir:  phar://wp-cli.phar/vendor
WP_CLI phar path:   /Users/mike/Docker/xamp/www/wordpress_wwws/htdocs
WP-CLI packages dir:    
WP-CLI global config:   
WP-CLI project config:  
WP-CLI version: 1.3.0

Any help or suggestions will be much appreciated.

Thanks

1 Answer 1

12

Regarding the wp --info output, that makes sense. If you don't have any packages installed (see wp package --help or a global configuration files (wp-cli.yml) then those items would be blank.

You can run the wp command from any location. If you're anywhere within your website's folder structure it will automatically detect the site you're on by navigating up the folder tree until it finds a wp-config.php file.

If running wp from a location outside your site's folder structure, you'll have to specify the path, like this:

wp --path=/path/to/wordpress/site/ core version

As for executing commands from within PHP, this following works for me:

test.php

<?php
$output = shell_exec("wp --info");
echo "<pre>".$output."</pre>";
?>

running php test.php from the command line, I get:

PHP binary:    /usr/local/Cellar/php71/7.1.1_12/bin/php
PHP version:    7.1.1
php.ini used:   /usr/local/etc/php/7.1/php.ini
WP-CLI root dir:    phar://wp-cli.phar
WP-CLI vendor dir:  phar://wp-cli.phar/vendor
WP_CLI phar path:   /Users/shawnhooper
WP-CLI packages dir:    /Users/shawnhooper/.wp-cli/packages/
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 1.3.0

Also there is quite a lengthy discussion on running WP-CLI commands from PHP in the project's official GitHub repo. You might find this issue of interest: https://github.com/wp-cli/wp-cli/issues/1924

1
  • Thanks Shawn, the suggestions on the WP-CLI github page helped me execute commands from the the PHP script.
    – WKL
    Commented Aug 21, 2017 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.