How to use WP_CLI::confirm() in WP CLI

Advertisement

In this article let’s learn about WP_CLI::confirm() function.

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

Create Empty Plugin Create Empty Plugin

  • Create a new folder wordpress-examples into plugins directory /wp-content/plugins/
  • Create a file wordpress-examples.php and add the below code into it.
<?php
 /**
 Plugin Name: WordPress Examples
 */ 

Note: If you want to add some additional information then you can get it from gist snippet – WordPress Complete Plugin Readme File.

Now you can see our WordPress Examples plugin exists into the plugins list.

Now, Activate the WordPress Examples plugin.

Top ↑

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 sub command Add confirm sub command

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

Here,

We have added a function confirm() 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

You can see the output something like below:

 λ wp examples confirm
 Hello

This command just shows the string Hello.

Top ↑

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

Its time to use WP_CLI::confirm().

public function confirm( $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.

D:\xampp\htdocs\dev.test
 λ wp examples confirm
 Do you want to proceed? [y/n]

Here, the terminal shows the message Do you want to proceed? and wait for our response.

Terminal wait for [y/n]values from us.

Let’s see how it responds if we type y:

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

Here, we see the message Great! You have confirmed to proceed! because we have typed the y and press the enter.

Let’s see how it responds if we type n:

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

Here, Our code is not executed and the terminal terminate the execution.

So, we can use WP_CLI::confirm() to perform such actions that need user permissions.

How to use WP_CLI::confirm() in WP CLI.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: