Using wp-cli through PHP

2019-07-25 17:55发布

Can I run WP-CLI https://wp-cli.org/ command through PHP script, so I can install user selected WordPress themes automatically with PHP script

2条回答
Juvenile、少年°
2楼-- · 2019-07-25 18:47

You can do something like this:

exec('wp site list --field=url --archived=0', $output);

foreach($output as $url) {
    echo $url . "\n";
    echo exec("wp --url=$url plugin activate this-plugin");
}

Run it in a PHP script in your home directory.

查看更多
beautiful°
3楼-- · 2019-07-25 18:48

As long as you can use the exec() or similar command. I use something like this to output the result of a wp-cli command:

<pre>
<?php
  exec("wp --info", $result);
  echo implode(PHP_EOL, $result); // join multi-line return result
?>
</pre>

Or simply:

<?php exec("wp --info");

See this answer: https://wordpress.stackexchange.com/questions/219230/utilize-wp-cli-from-inside-wordpress-not-ssh for further discussion.

查看更多
登录 后发表回答