1
  1. I have Zend framework 2 application (PHP), via which I want to use WP-CLI functionality. near Zend project I have WordPress project, which I want to maintain from Zend via WP-CLI.
  2. I see in the docs that WP-CLI also written on PHP. I dreamed that I install WP-CLI via composer in root of my project and can use its classes there.
  3. After installing via composer in WP-CLI's sources I see functions from WordPress (like as is_multisite and etc) and I little disappointed :).

main question: Can I in some way use WP-CLI sources directly from my Zend-project without calling commands via terminal? for example (in abstract programming language):

$command = new WP_CLI::command('command_name subcommand_name', $params, $assoc_params, .....);

$result = $command->execute();

Or WP-CLI was made only as part of the WordPress project as way for extending it's commands and it is unable to use them as I am trying?

2 Answers 2

1

This is probably unwise. WP-CLI is developed as a command line utility and might not maintain internal structure between releases.

Since everything has to run as root in any case, there is no real difference between executing a WP-CLI command via a shell (A.K.A exec and its family of functions), or by calling whatever API.

0

The WP_CLI class has a runcommand method that launches a new child process to run a specified WP-CLI command. According to the docs, you can use it like this:

$options = array(
  'return'     => true,   // Return 'STDOUT'; use 'all' for full object.
  'parse'      => 'json', // Parse captured STDOUT to JSON array.
  'launch'     => false,  // Reuse the current process.
  'exit_error' => true,   // Halt script execution on error.
);

$plugins = WP_CLI::runcommand( 'plugin list --format=json', $options );

Your Answer

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

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