-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new block development "Quick Start Guide" and update the `create-…
…block-tutorial-template` (#56056) * Add the new quick start guide. * Update the create-block tutorial template to work with the new quick start guide. * Remove the quick start guide from the create block tutorial. * Add back ABSPATH check. * Revert change to package version number. * Update readme. * Remove @wordpress/icons dependency and add custom icon. * Fix title in manifest. * Fix heading in Quick Start Guide.
- Loading branch information
Showing
17 changed files
with
233 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Quick Start Guide | ||
|
||
This guide is designed to demonstrate the basic principles of block development in WordPress using a hands-on approach. Following the steps below, you will create a custom block plugin that uses modern JavaScript (ESNext and JSX) in a matter of minutes. The example block displays the copyright symbol (©) and the current year, the perfect addition to any website's footer. | ||
|
||
## Scaffold the block plugin | ||
|
||
Start by ensuring you have Node.js and `npm` installed on your computer. Review the [Node.js development environment](https://developer.wordpress.org/block-editor/getting-started/devenv/nodejs-development-environment/) guide if not. | ||
|
||
Next, use the [`@wordpress/create-block`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/) package and the [`@wordpress/create-block-tutorial-template`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block-tutorial-template/) template to scaffold the complete “Copyright Date Block” plugin. | ||
|
||
<div class="callout callout-info"> | ||
<p>You can use <code>create-block</code> to scaffold a block just about anywhere and then use <a href="https://developer.wordpress.org/block-editor/getting-started/devenv/get-started-with-wp-env/"><code>wp-env</code></a> inside the generated plugin folder. This will create a local WordPress development environment with your new block plugin installed and activated.</p> | ||
<p>If you already have your own <a href="https://developer.wordpress.org/block-editor/getting-started/devenv/#local-wordpress-environment">local WordPress development environment</a>, navigate to the <code>plugins/</code> folder using the terminal.</p> | ||
</div> | ||
|
||
Choose the folder where you want to create the plugin, and then execute the following command in the terminal from within that folder: | ||
|
||
```sh | ||
npx @wordpress/create-block copyright-date-block --template create-block-tutorial-template | ||
``` | ||
|
||
The `slug` provided (`copyright-date-block`) defines the folder name for the scaffolded plugin and the internal block name. | ||
|
||
Navigate to the Plugins page of your local WordPress installation and activate the “Copyright Date Block” plugin. The example block will then be available in the Editor. | ||
|
||
## Basic usage | ||
|
||
With the plugin activated, you can explore how the block works. Use the following command to move into the newly created plugin folder and start the development process. | ||
|
||
```sh | ||
cd copyright-date-block && npm start | ||
``` | ||
|
||
When `create-block` scaffolds the block, it installs `wp-scripts` and adds the most common scripts to the block’s `package.json` file. Refer to the [Get started with wp-scripts](https://developer.wordpress.org/block-editor/getting-started/devenv/get-started-with-wp-scripts/) article for an introduction to this package. | ||
|
||
The `npm start` command will start a development server and watch for changes in the block’s code, rebuilding the block whenever modifications are made. | ||
|
||
When you are finished making changes, run the `npm run build` command. This optimizes the block code and makes it production-ready. | ||
|
||
## Additional resources | ||
|
||
- [Get started with create-block](https://developer.wordpress.org/block-editor/getting-started/devenv/get-started-with-create-block/) | ||
- [Get started with wp-scripts](https://developer.wordpress.org/block-editor/getting-started/devenv/get-started-with-wp-scripts/) | ||
- [Get started with wp-env](https://developer.wordpress.org/block-editor/getting-started/devenv/get-started-with-wp-env/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
packages/create-block-tutorial-template/block-templates/editor.scss.mustache
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 29 additions & 6 deletions
35
packages/create-block-tutorial-template/block-templates/render.php.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,33 @@ | ||
{{#isDynamicVariant}} | ||
<?php | ||
/** | ||
* PHP file to use when rendering the block type on the server to show on the front end. | ||
* | ||
* The following variables are exposed to the file: | ||
* $attributes (array): The block attributes. | ||
* $content (string): The block default content. | ||
* $block (WP_Block): The block instance. | ||
* | ||
* @see https://github.com/WordPress/gutenberg/blob/trunk/docs/reference-guides/block-api/block-metadata.md#render | ||
*/ | ||
?> | ||
<p <?php echo get_block_wrapper_attributes(); ?>> | ||
<?php echo esc_html( $attributes['message'] ); ?> | ||
</p> | ||
{{/isDynamicVariant}} | ||
|
||
// Get the current year. | ||
$current_year = date( "Y" ); | ||
|
||
// Determine which content to display. | ||
if ( isset( $attributes['fallbackCurrentYear'] ) && $attributes['fallbackCurrentYear'] === $current_year ) { | ||
// The current year is the same as the fallback, so use the block content saved in the database (by the save.js function). | ||
$block_content = $content; | ||
} else { | ||
// The current year is different from the fallback, so render the updated block content. | ||
if ( ! empty( $attributes['startingYear'] ) && ! empty( $attributes['showStartingYear'] ) ) { | ||
$display_date = $attributes['startingYear'] . '–' . $current_year; | ||
} else { | ||
$display_date = $current_year; | ||
} | ||
|
||
$block_content = '<p ' . get_block_wrapper_attributes() . '>© ' . esc_html( $display_date ) . '</p>'; | ||
} | ||
|
||
echo wp_kses_post( $block_content ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.