自作処理を行うwp cliコマンドを作成して実行する方法をご紹介します。WordPressとは無関係にphpで作ってもよいのですが、WP-CLIであればカスタム投稿やカスタムタクソノミー などWPの機能に簡単にアクセスできるので便利です。
テーマのfunctions.phpにコマンド追加の指示を書きます。
(composerでインストールしたため、パスがvendorになっています。)
$autoload = __DIR__.'/../../../vendor/wp-cli/wp-cli/php/class-wp-cli.php';
if (file_exists($autoload)) {
require_once $autoload;
require_once 'test_command1.php';
WP_CLI::add_command( 'test_command1', 'TestCommand1' );
}
実行するクラス(test_command1.php)を用意してあげます。
これで wp cli test_command1というコマンドで呼び出せるようになります。
%3C%3Fphp
class TestCommand1 {
/**
* Prints a greeting.
*
* ## OPTIONS
*
* ## EXAMPLES
*
* wp test_command1
*
* @when after_wp_load
*/
function get( $args, $assoc_args ) {
WP_CLI::success( "Hello" );
}
}
EC-CUBEゴールドパートナー