1

I'm looking for a way to test out some WP-CLI commands but from the Debug Bar Console where I normally test PHP. For cases when I don't want to switch windows to SSH, this might be a way to reuse/develop effective CLI methods outside of the CLI.

I've seen exec but it only shows me what appears to be the last line of the command.

echo exec('ls -la'); // -rw-rw-r-- 1 www-data www-data 18108 Jan 20 21:57 widgets.php

Has anyone tried to string a more complex set of commands together and in a way that is more human-readable?

1

WP-CLI is not different from any other server side utilities in that you need to be able to have the permission to run them out of a webserver enviroment using exec, spawn or friends. For obvious reasons all those kinds of PHP APIs are going to be blocked on most servers and therefor it is unlikely that a server you do not manage (don't have SSH access is an indication) will let you run WP-CLI out of wordpress.

  • Sorry, was thinking blue sky there for a minute with the whole non-SSH access. The real reason is that I'm lazy and don't want to manage a bunch of windows if I can quickly pull some data from the console. Looks like it's working now and giving me all the info I was hoping for. – jgraup Feb 29 '16 at 5:15
  • I forgot I have a terminal window in PHPStorm... so... I guess this was more a thought exercise. – jgraup Feb 29 '16 at 16:43
0

It looks like exec() might still work with the right variables.

$last_line = exec( $command, &$output, &$return_var )

The second parameter captures all the return data while the return from the function captures the last line.



$commands = array(
    'wp --version',
    'whoami',
    'pwd',
    'ls -la',
    'wp theme list',
    'wp plugin list',
);

echo '<pre>'.PHP_EOL;
foreach ($commands as $command ) { 

    // run command
    exec($command, $retval);

    // convert output array to text
    echo implode(PHP_EOL, $retval) . PHP_EOL;

    // clear the var for the next command
    unset($retval);
}
echo '</pre>'; 
  • What should I do with it? sustitute the arrays list by the CLI install commands? – Milor123 Oct 13 '17 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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