In a recent article, we learn about the $assoc_args and $args in the WP CLI program.
Now, Let’s try some examples of $args and $assoc_args.
Note: If you read some recent articles then you can skip some below steps.
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 plugin.
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
.
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
.
Add arguments
sub command Add arguments
sub command
public function arguments( $args, $assoc_args ) { WP_CLI::line( 'Hello' ); }
Here,
We have added a function arguments()
with two parameters.
$args
contain the arguments.$assoc_args
contain the associate arguments.
Testing Testing
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 arguments
You can see the output something like below:
? wp examples arguments Hello
This command just shows the string Hello
. Now, Let’s try some examples:
Example 1
public function arguments( $args, $assoc_args ) { WP_CLI::line( 'Hello' ); WP_CLI::line( print_r( $args ) ); WP_CLI::line( print_r( $assoc_args ) ); }
Let’s execute our command wp examples associated_arguments
.
? wp examples arguments Hello Array ( ) 1 Array ( ) 1
Here, $args
and $assoc_args
both are empty. So, After executing above command WP_CLI::line( print_r( $args ) );
and WP_CLI::line( print_r( $assoc_args ) );
both print an empty array.
Now, Execute
But, If instead of command:
wp examples arguments One Two --one=One --two=Two
? wp examples arguments One Two --one=One --two=Two Hello Array ( [0] => One [1] => Two ) 1 Array ( [one] => One [two] => Two ) 1
Now, We see that both $args
and $assoc_args
contain some values.
If we execute the below command by changing the sequence of the arguments and associate arguments like:
wp examples arguments One --one=One Two --two=Two
? wp examples arguments One --one=One Two --two=Two Hello Array ( [0] => One [1] => Two ) 1 Array ( [one] => One [two] => Two ) 1
Here, There is no change. Because there is no sequence of for passing the arguments and associate arguments.
Using \WP_CLI\Utils\get_flag_value() Using \WP_CLI\Utils\get_flag_value()
WP CLI has some helper functions which are more useful while developing some commands.
The \WP_CLI\Utils\get_flag_value()
is one of them which allow us to get the value of the $args
and $assoc_args
.
Let’s see it with a below simple example.
public function arguments( $args, $assoc_args ) { $first = isset( $args[0] ) ? $args[0] : 'First Argument'; $last = isset( $args[1] ) ? $args[1] : 'Last Argument'; WP_CLI::line( $first ); WP_CLI::line( $last ); // Or we can use the `\WP_CLI\Utils\get_flag_value()` which return the same. $first = \WP_CLI\Utils\get_flag_value( $assoc_args, 'first', 'First Associate Argument' ); $last = \WP_CLI\Utils\get_flag_value( $assoc_args, 'last', 'Last Associate Argument' ); WP_CLI::line( $first ); WP_CLI::line( $last ); }
If we execute the command wp examples arguments
then we see something like below:
? wp examples arguments First Argument Last Argument First Associate Argument Last Associate Argument
Let’s try the command wp examples arguments Mahesh --first=Swapnil Waghmare --last=Dhanrale
? wp examples arguments Mahesh --first=Swapnil Waghmare --last=Dhanrale Mahesh Waghmare Swapnil Dhanrale
As we know that there is no sequence for passing the arguments and associate arguments in the WP CLI command.
wp cli arguments vs associated arguments.
Tweet