By WKL


2017-08-18 19:51:30 8 Comments

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 comments

@Shawn H 2017-08-18 21:01:11

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

@WKL 2017-08-21 17:01:04

Thanks Shawn, the suggestions on the WP-CLI github page helped me execute commands from the the PHP script.

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] WP-CLI - Selecting PHP version

  • 2016-10-27 14:20:51
  • j03
  • 3463 View
  • 3 Score
  • 3 Answer
  • Tags:   php errors wp-cli

1 Answered Questions

[SOLVED] wp-cli configuration in php?

  • 2019-06-05 13:53:47
  • fifi finance
  • 24 View
  • 1 Score
  • 1 Answer
  • Tags:   wp-cli

1 Answered Questions

[SOLVED] Failed to open stream / no such file or directory

  • 2017-03-28 21:25:10
  • jomar
  • 12883 View
  • 0 Score
  • 1 Answer
  • Tags:   php installation

3 Answered Questions

[SOLVED] wp-cli is not working properly. No response from commands

  • 2014-04-21 03:25:12
  • jeff
  • 2700 View
  • 4 Score
  • 3 Answer
  • Tags:   multisite wp-cli

1 Answered Questions

[SOLVED] wp-cli: run post generate and post meta add together

  • 2015-11-20 21:00:19
  • marcelo2605
  • 588 View
  • 2 Score
  • 1 Answer
  • Tags:   wp-cli

1 Answered Questions

[SOLVED] Wordpress test environment on windows

  • 2014-05-03 15:39:31
  • No3x
  • 1127 View
  • 7 Score
  • 1 Answer
  • Tags:   unit-tests wp-cli

1 Answered Questions

[SOLVED] wp-cli 0.14.1 MySQL error

  • 2014-02-28 22:11:45
  • user1640896
  • 559 View
  • 8 Score
  • 1 Answer
  • Tags:   wp-cli

1 Answered Questions

[SOLVED] wp-cli doesn't find and wordpress installation

  • 2014-01-12 20:56:59
  • step21
  • 5685 View
  • 4 Score
  • 1 Answer
  • Tags:   wp-cli

Sponsored Content