Skip to content

Instantly share code, notes, and snippets.

@thaicloud
Last active June 29, 2016 19:09
Show Gist options
  • Save thaicloud/8c12950f9ab64fb64a18 to your computer and use it in GitHub Desktop.
Save thaicloud/8c12950f9ab64fb64a18 to your computer and use it in GitHub Desktop.
Create a dummy WP-CLI command, passing 1 parameter
<?php
/**
*
* Plugin Name: Demo - MJ WP-CLI Command
* Description: Create WP CLI Command
* Author: MJ
* Version: 1.0
*/
namespace MJ\Migration;
/**
* Class Migration
* @package MJ\Migration
*/
class Migration {
/**
* Registers all the hooks bla bla
*/
function __construct() {
}
/**
* @param $param
*/
function run_import( $param ) {
if ( empty( $param ) ) {
$param = 'nothing set';
}
\WP_CLI::line( esc_html( "param is $param " ) );
}
}
/**
* Class Example_Command
* @package MJ\Migration
*
* Generate our command object and give it a sub-command 'import'
* Usage: wp example-command import --param=bob
*/
class Example_Command extends \WP_CLI_Command {
/**
* @param $args
* @param $assoc_args
*/
public function import( $args, $assoc_args ) {
$demoObject = new Migration();
$demoObject->run_import( $assoc_args['param'] );
\WP_CLI::success( "All done!" );
}
}
// Make sure WP-CLI exists
if ( defined( 'WP_CLI' ) && \WP_CLI ) {
// Our new command will be 'example-command'
\WP_CLI::add_command( 'example-command', __NAMESPACE__ . '\\Example_Command' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment