Skip to content

Commit 2e20bff

Browse files
Merge pull request #179 from wp-cli/include-example-command
Include an example command on the homepage
2 parents 70e9cd2 + 5c7243f commit 2e20bff

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

index.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,36 @@ If you have a WordPress.org account, you may also consider joining the `#cli` ch
125125

126126
A **command** is an atomic unit of WP-CLI functionality. `wp plugin install` ([doc](https://wp-cli.org/commands/plugin/install/)) is one command. `wp plugin activate` ([doc](https://wp-cli.org/commands/plugin/activate/)) is another.
127127

128+
WP-CLI supports registering any callable class, function, or closure as a command. It reads usage details from the callback's PHPdoc. `WP_CLI::add_command()` ([doc](https://wp-cli.org/docs/internal-api/wp-cli-add-command/)) is used for both internal and third-party command registration.
129+
130+
```
131+
/**
132+
* Delete an option from the database.
133+
*
134+
* Returns an error if the option didn't exist.
135+
*
136+
* ## OPTIONS
137+
*
138+
* <key>
139+
* : Key for the option.
140+
*
141+
* ## EXAMPLES
142+
*
143+
* $ wp option delete my_option
144+
* Success: Deleted 'my_option' option.
145+
*/
146+
$delete_option_cmd = function( $args ) {
147+
list( $key ) = $args;
148+
149+
if ( ! delete_option( $key ) ) {
150+
WP_CLI::error( "Could not delete '$key' option. Does it exist?" );
151+
} else {
152+
WP_CLI::success( "Deleted '$key' option." );
153+
}
154+
};
155+
WP_CLI::add_command( 'option delete', $delete_option_cmd );
156+
```
157+
128158
WP-CLI comes with dozens of commands. It's easier than it looks to create a custom WP-CLI command. Read the [commands cookbook](https://wp-cli.org/docs/commands-cookbook/) to learn more. Browse the [internal API docs](https://wp-cli.org/docs/internal-api/) to discover a variety of helpful functions you can use in your custom WP-CLI command.
129159

130160
## Contributing

0 commit comments

Comments
 (0)