WP_CLI::confirm() with Associate Argument

Advertisement

In this article we are going to learn about WP_CLI::confirm() with associate argument.

Note: If you read some recent articles then you can skip some below steps that are related to plugin creation.

Register WP CLI Command Register WP CLI Command

Register PHP class WordPress_Examples_WP_CLI e.g.

if ( ! class_exists( 'WordPress_Examples_WP_CLI' ) && class_exists( 'WP_CLI_Command' ) ) :
       class WordPress_Examples_WP_CLI extends WP_CLI_Command {

       }
endif;

Here,

We have registered a new class WordPress_Examples_WP_CLI and extend it with WP_CLI_Command.

Top ↑

Add Examples Command Add Examples Command

Now let’s register the examples. E.g.

       WP_CLI::add_command( 'examples', 'WordPress_Examples_WP_CLI' ); 

Here,

We have used the function WP_CLI::add_command() to register our examples command.

The function WP_CLI::add_command() accepts 2 parameters. The first parameter is the command name. Which is examples in our case.

And second is a callback class which is WordPress_Examples_WP_CLI.

Top ↑

Add confirm_with_flag sub command Add confirm_with_flag sub command

         public function confirm_with_flag( $args, $assoc_args ) {
           WP_CLI::line( 'Hello' );
         }

Here,

We have added a function confirm_with_flag() with two parameters.

  • $args contain the arguments.
  • $assoc_args contain the associate arguments.

Top ↑

Testing the Command Testing the Command

Open command prompt/terminal. Go to your WordPress setup. I have set up WordPress in D:\xampp\htdocs\dev.test

So, Execute the below commands:

  • cd D:\xampp\htdocs\dev.test
  • wp examples confirm_with_flag

You can see the output something like below:

 ? wp examples confirm_with_flag
 Hello

This command just shows the string Hello.

Top ↑

Using WP_CLI::confirm() Using WP_CLI::confirm()

Its time use WP_CLI::confirm().

public function confirm_with_flag( $args, $assoc_args ) {
	WP_CLI::confirm( 'Do you want to proceed?' );
	WP_CLI::line( 'Great! You have confirm to proceed!' );
}

Let’s execute the command wp examples confirm_with_flag and type y

? wp examples confirm_with_flag
 Do you want to proceed? [y/n] y
 Great! You have confirm to proceed!

Here, We have seen the string Great! You have confirm to proceed! because we type y and press the enter key.

Now, Let’s execute the command wp examples confirm_with_flag and type n

? wp examples confirm_with_flag
 Do you want to proceed? [y/n] n

Here, By pressing a key n we have terminated the WP CLI command execution.

Now, We are going to add --yes associate argument to avoid the [y/n] user inputs.

If someone type --yes and execute the command then we process the command execution. Otherwise, we ask the confirm message.

public function confirm_with_flag( $args, $assoc_args ) {
	// Confirm before proceed.
	$yes = isset( $assoc_args['yes'] ) ? true : false;
	if ( ! $yes ) {
		WP_CLI::confirm( 'Do you want to proceed?' );
	}
	WP_CLI::line( 'Great! You have confirm to proceed!' );
}

Now execute command wp examples confirm_with_flag

? wp examples confirm_with_flag
 Do you want to proceed? [y/n] y
 Great! You have confirm to proceed!

Here, It works the same as recent work. Now, let’s try it by adding the associate argument --yes like: wp examples confirm_with_flag --yes

? wp examples confirm_with_flag --yes
 Great! You have confirm to proceed!

Great! Now, The confirm message is not asking users to execute the command or not.

Such kind of flags will help us while debugging or when we intentionally want to execute the command.

Using WP_CLI::confirm() with an associate argument in WP CLI.

Leave a Reply