0

How can I run a wp cli command when a wordpress hook is called on my website.

On user registration hook, I would like to run a wp cli command to activate a license.

  • 3
    WP ClI has it's name suggests is a command line utility. It's not made to be run from php. Consider editing your question further, add some more information on what you are trying, does it involve a database operation, a file operation, a post request? – Toffee Mar 12 at 12:57
1

Depends on what you want to do. If you want to run the PHP code that's behind the WP-CLI code you might consider looking at https://github.com/wp-cli/entity-command

Maybe you don't actually need WP-CLI but the corresponding code behind it. Most WP-CLI commands have Wordpress equivalents. For example of what I was trying to do today, the wp menu create "My Menu" command is defined here: https://github.com/wp-cli/entity-command/blob/master/src/Menu_Command.php. (It was much easier to find documentation for WP-CLI for this because 99% of results for Wordpress describe how to do it via the admin panel.)

Basically it just uses the WP function:

65:     $menu_id = wp_create_nav_menu( $args[0] );

So > wp menu create "My Menu" on the commandline is roughly equivalent to wp_create_nav_menu('My Menu') in a functions.php file.

Similarly, the plugin command would be addressed as a WP-CLI extension command defined in Plugin_Command.php which uses the Wordpress command activate_plugin(). If you want to know the WP version of the WP-CLI command you could look it up yourself or include the specific command you want to know in your question.

| improve this answer | |
0

You can use the PHP exec() function to run wp-cli commands.

Example: <?php exec('wp core download'); ?>

Use with care. Further reading:

https://www.php.net/manual/en/function.exec.php

| improve this answer | |

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.