public function block( $args, $assoc_args ) {

		$slug = $args[0];
		if ( ! preg_match( '/^[a-z][a-z0-9\-]*$/', $slug ) ) {
			WP_CLI::error( 'Invalid block slug specified. Block slugs can contain only lowercase alphanumeric characters or dashes, and start with a letter.' );
		}

		$defaults = array(
			'title'    => str_replace( '-', ' ', $slug ),
			'category' => 'widgets',
		);
		$data     = $this->extract_args( $assoc_args, $defaults );

		$data['slug']             = $slug;
		$data['title_ucfirst']    = ucfirst( $data['title'] );
		$data['title_ucfirst_js'] = esc_js( $data['title_ucfirst'] );

		$dashicon = $this->extract_dashicon( $assoc_args );
		if ( $dashicon ) {
			$data['dashicon'] = $dashicon;
		}

		$control_defaults = array(
			'force'  => false,
			'plugin' => false,
			'theme'  => false,
		);
		$control_args     = $this->extract_args( $assoc_args, $control_defaults );

		if ( isset( $control_args['plugin'] ) ) {
			if ( ! preg_match( '/^[A-Za-z0-9\-]*$/', $control_args['plugin'] ) ) {
				WP_CLI::error( 'Invalid plugin name specified. The block editor can only register blocks for plugins that have nothing but lowercase alphanumeric characters or dashes in their slug.' );
			}
		}

		$data['namespace']    = $control_args['plugin'] ? $control_args['plugin'] : $this->get_theme_name( $control_args['theme'] );
		$data['machine_name'] = $this->generate_machine_name( $slug );
		$data['plugin']       = $control_args['plugin'] ? true : false;
		$data['theme']        = ! $data['plugin'];

		$block_dir = $this->get_output_path( $control_args, '/blocks' );
		if ( ! $block_dir ) {
			WP_CLI::error( 'No plugin or theme selected.' );
		}

		$files_to_create = array(
			"{$block_dir}/{$slug}.php"        => self::mustache_render( 'block-php.mustache', $data ),
			"{$block_dir}/{$slug}/index.js"   => self::mustache_render( 'block-index-js.mustache', $data ),
			"{$block_dir}/{$slug}/editor.css" => self::mustache_render( 'block-editor-css.mustache', $data ),
			"{$block_dir}/{$slug}/style.css"  => self::mustache_render( 'block-style-css.mustache', $data ),
		);
		$files_written   = $this->create_files( $files_to_create, $control_args['force'] );
		$skip_message    = 'All block files were skipped.';
		$success_message = "Created block '{$data['title_ucfirst']}'.";
		$this->log_whether_files_written( $files_written, $skip_message, $success_message );
	}
public function block( $args, $assoc_args ) {

		$slug = $args[0];
		if ( ! preg_match( '/^[a-z][a-z0-9\-]*$/', $slug ) ) {
			WP_CLI::error( 'Invalid block slug specified. Block slugs can contain only lowercase alphanumeric characters or dashes, and start with a letter.' );
		}

		$defaults = array(
			'title'    => str_replace( '-', ' ', $slug ),
			'category' => 'widgets',
		);
		$data     = $this->extract_args( $assoc_args, $defaults );

		$data['slug']             = $slug;
		$data['title_ucfirst']    = ucfirst( $data['title'] );
		$data['title_ucfirst_js'] = esc_js( $data['title_ucfirst'] );

		$dashicon = $this->extract_dashicon( $assoc_args );
		if ( $dashicon ) {
			$data['dashicon'] = $dashicon;
		}

		$control_defaults = array(
			'force'  => false,
			'plugin' => false,
			'theme'  => false,
		);
		$control_args     = $this->extract_args( $assoc_args, $control_defaults );

		if ( isset( $control_args['plugin'] ) ) {
			if ( ! preg_match( '/^[A-Za-z0-9\-]*$/', $control_args['plugin'] ) ) {
				WP_CLI::error( 'Invalid plugin name specified. The block editor can only register blocks for plugins that have nothing but lowercase alphanumeric characters or dashes in their slug.' );
			}
		}

		$data['namespace']    = $control_args['plugin'] ? $control_args['plugin'] : $this->get_theme_name( $control_args['theme'] );
		$data['machine_name'] = $this->generate_machine_name( $slug );
		$data['plugin']       = $control_args['plugin'] ? true : false;
		$data['theme']        = ! $data['plugin'];

		$block_dir = $this->get_output_path( $control_args, '/blocks' );
		if ( ! $block_dir ) {
			WP_CLI::error( 'No plugin or theme selected.' );
		}

		$files_to_create = array(
			"{$block_dir}/{$slug}.php"        => self::mustache_render( 'block-php.mustache', $data ),
			"{$block_dir}/{$slug}/index.js"   => self::mustache_render( 'block-index-js.mustache', $data ),
			"{$block_dir}/{$slug}/editor.css" => self::mustache_render( 'block-editor-css.mustache', $data ),
			"{$block_dir}/{$slug}/style.css"  => self::mustache_render( 'block-style-css.mustache', $data ),
		);
		$files_written   = $this->create_files( $files_to_create, $control_args['force'] );
		$skip_message    = 'All block files were skipped.';
		$success_message = "Created block '{$data['title_ucfirst']}'.";
		$this->log_whether_files_written( $files_written, $skip_message, $success_message );
	}
public function block ( $ args , $ assoc_args ) { $ slug = $ args [ 0 ] ; if ( ! preg_match ( '/^[a-z][a-z0-9\-]*$/' , $ slug ) ) { WP_CLI :: error ( 'Invalid block slug specified. Block slugs can contain only lowercase alphanumeric characters or dashes, and start with a letter.' ) ; } $ defaults = array ( 'title' => str_replace ( '-' , ' ' , $ slug ) , 'category' => 'widgets' , ) ; $ data = $ this -> extract_args ( $ assoc_args , $ defaults ) ; $ data [ 'slug' ] = $ slug ; $ data [ 'title_ucfirst' ] = ucfirst ( $ data [ 'title' ] ) ; $ data [ 'title_ucfirst_js' ] = esc_js ( $ data [ 'title_ucfirst' ] ) ; $ dashicon = $ this -> extract_dashicon ( $ assoc_args ) ; if ( $ dashicon ) { $ data [ 'dashicon' ] = $ dashicon ; } $ control_defaults = array ( 'force' => false , 'plugin' => false , 'theme' => false , ) ; $ control_args = $ this -> extract_args ( $ assoc_args , $ control_defaults ) ; if ( isset ( $ control_args [ 'plugin' ] ) ) { if ( ! preg_match ( '/^[A-Za-z0-9\-]*$/' , $ control_args [ 'plugin' ] ) ) { WP_CLI :: error ( 'Invalid plugin name specified. The block editor can only register blocks for plugins that have nothing but lowercase alphanumeric characters or dashes in their slug.' ) ; } } $ data [ 'namespace' ] = $ control_args [ 'plugin' ] ? $ control_args [ 'plugin' ] : $ this -> get_theme_name ( $ control_args [ 'theme' ] ) ; $ data [ 'machine_name' ] = $ this -> generate_machine_name ( $ slug ) ; $ data [ 'plugin' ] = $ control_args [ 'plugin' ] ? true : false ; $ data [ 'theme' ] = ! $ data [ 'plugin' ] ; $ block_dir = $ this -> get_output_path ( $ control_args , '/blocks' ) ; if ( ! $ block_dir ) { WP_CLI :: error ( 'No plugin or theme selected.' ) ; } $ files_to_create = array ( "{$block_dir}/{$slug}.php" => self :: mustache_render ( 'block-php.mustache' , $ data ) , "{$block_dir}/{$slug}/index.js" => self :: mustache_render ( 'block-index-js.mustache' , $ data ) , "{$block_dir}/{$slug}/editor.css" => self :: mustache_render ( 'block-editor-css.mustache' , $ data ) , "{$block_dir}/{$slug}/style.css" => self :: mustache_render ( 'block-style-css.mustache' , $ data ) , ) ; $ files_written = $ this -> create_files ( $ files_to_create , $ control_args [ 'force' ] ) ; $ skip_message = 'All block files were skipped.' ; $ success_message = "Created block '{$data['title_ucfirst']}'." ; $ this -> log_whether_files_written ( $ files_written , $ skip_message , $ success_message ) ; }
Generates PHP, JS and CSS code for registering a Gutenberg block for a plugin or theme.

Blocks are the fundamental element of the Gutenberg editor. They are the primary way in which plugins and themes can register their own functionality and extend the capabilities of the editor.

Visit the [Gutenberg handbook](https://wordpress.org/gutenberg/handbook/block-api/) to learn more about Block API.

When you scaffold a block you must use either the theme or plugin option. The latter is recommended.

## OPTIONS

<slug>
: The internal name of the block.

[--title=<title>]
: The display title for your block.

[--dashicon=<dashicon>]
: The dashicon to make it easier to identify your block.

[--category=<category>]
: The category name to help users browse and discover your block.
---
default: widgets
options:
- common
- embed
- formatting
- layout
- widgets
---

[--theme]
: Create files in the active theme directory. Specify a theme with `--theme=<theme>` to have the file placed in that theme.

[--plugin=<plugin>]
: Create files in the given plugin's directory.

[--force]
: Overwrite files that already exist.

## EXAMPLES

# Generate a 'movie' block for the 'movies' plugin
$ wp scaffold block movie --title="Movie block" --plugin=movies
Success: Created block 'Movie block'.

# Generate a 'movie' block for the 'simple-life' theme
$ wp scaffold block movie --title="Movie block" --theme=simple-life
Success: Created block 'Movie block'.

# Create a new plugin and add two blocks
# Create plugin called books
$ wp scaffold plugin books
# Add a block called book to plugin books
$ wp scaffold block book --title="Book" --plugin=books
# Add a second block to plugin called books.
$ wp scaffold block books --title="Book List" --plugin=books

@subcommand block
Generates PHP JS and CSS code for registering a Gutenberg block for a plugin or theme .