Skip to content
biukop.com 1800 181 522

this is a development document on how to implement WordPress backend command line triggers, so as to do scheduled tasks.

WP-CLI

https://wp-cli.org/

WP-CLI is a project specifically developed on operating WordPress from the command line. It has been widely used by backend developers to implement tasks that are not triggered by website visitors.

We need a simple plugin to trigger it from inside WordPress, if we don’t want to develop a plugin, we can patch the following code into any plugin we like.

// 1. Command is a function
function foo_command( $args ) {
    WP_CLI::success( $args[0] );
}
WP_CLI::add_command( 'foo', 'foo_command' );

// 2. Command is a closure
$foo_command = function( $args ) {
    WP_CLI::success( $args[0] );
}
WP_CLI::add_command( 'foo', $foo_command );

// 3. Command is a method on a class
class Foo_Command {
    public function __invoke( $args ) {
        WP_CLI::success( $args[0] );
    }
}
WP_CLI::add_command( 'foo', 'Foo_Command' );

// 4. Command is a method on a class with constructor arguments
class Foo_Command {
    protected $bar;
    public function __construct( $bar ) {
        $this->bar = $bar;
    }
    public function __invoke( $args ) {
        WP_CLI::success( $this->bar . ':' . $args[0] );
    }
}
$instance = new Foo_Command( 'Some text' );
WP_CLI::add_command( 'foo', $instance );

 

With that entry point, we can trigger any functions inside WordPress as if people are visiting the WordPress website.

Trigger php command without WP-CLI

For HTOM, If we only housekeeping invoices, we do not need triggers from within WordPress. In those cases, we can directly call our PHP index.php file from the command line. If that’s the case, we only need to call our index.php from the command line of  HOTM  API  v1.3.

Do we need WordPress functions for jobs on a daily bases for HTOM?

Back To Top