This block was created to demonstrate how developers can utilize the Create Block package to scaffold a new block with the Interactivity API.
Create Block is an officially supported tool for scaffolding a WordPress plugin that registers a block. It generates PHP, JS, CSS code, and everything you need to start the project. It also integrates a modern build setup with no configuration.
This repository is organized so that each branch represents sequential changes to the codebase. Hopefully this helps developers review and learn how to create similar blocks.
Each branch should be name like this step-1
, step-2
, step-3
, etc.
Each branch's README
will provide the new changes made in that step.
As stated in the Wordpress Developer Resources -> Block Editor Handbook -> Reference Guides -> Package Reference -> @wordpress/interactivity
- Scaffold an interactive block
npx @wordpress/create-block@latest feedback-block --short-description "Feedback block for single posts" --template @wordpress/create-block-interactive-template
Running this command will create a new WordPress plugin with the necessary and helpful @wordpress/scripts
package installed as developer dependencies and the @wordpress/interactivity
as a dependency. It also creates a new custom block with the slug feedback-block
, and the --short-description "Feedback block for single posts"
for the plugin and the block.
- Update our block's
block.json
definitions to include"usesContext": [ "postId" ]
. Since we'll be placing our block on a single post type and we want to reference the postId for that post type then we'll need to pass it down in our block definitions. - Update the
render.php
for our block to do an early check forif ( ! isset( $block->context['postId'] ) )
- Add
$server_context
array to establish server side context that we can pass into and also establish local state (context
) for the Interactivity API's store. - Pass the
$server_context
intowp_interactivity_data_wp_context()
to establish client side local state. - Update the block's Interactivity API slug to align with our block's intent and custom functionality. This slug is a critical piece for initializing the Interactivity API on our DOM node.
- Remove the Create Block scaffold's use of
$unique_id
, as we will not need it here. - Update the Create Block's example
logIsOpen
callback tologPostId
so we can simply verify that everything is working in the console.
- Add a
$user_sentiments
array, which contains our feedback options (id
,label
,icon
). - Add in HTML markup with Interactivity API directives for the top area:
.feedback-block__trigger-bar
. These include buttons to toggle the form open and closed. - Add in CSS to start styling our
.feedback-block__trigger-bar
along with some CSS custom properties that we can reference.
- Add remaining form HTML markup with interactive directives.
- Start updating global state in
view.js
. - Add getters for
hasSuccess
,hasError
, andformMessage
instate
object. - Add
focusField
method to thecallbacks
object. This will set.focus()
on thetextarea
when the form area is expanded. - Add
toggleForm
method to theactions
object. This will handle toggling the form open and closed. - Add
*submitForm
generator function to theactions
object. This is where we handle the client-side form submission. - Add the remaining CSS for the form area.
- Add missing
getElement
import for@wordpress/interactivity
module.
- Add WordPress AJAX action to handle form submission. Check the nonce (number used once) for security. Trigger an email to
admin_email
if submission is successful. - Use Block Hooks to assign our feedback-block to
after
thecore/post-terms
block on a single post template.
- Rename registered block name:
create-block/feedback-block
->dev-hours/feedback-block
, and update CSS targets to match.
- Add an
.nvmrc
file for better developer experience. - CSS adjustments to status message for better color contrast.