Creating Custom Gutenberg Blocks in WooCommerce: A Complete Developer's Guide

WordPress recently launched @wordpress/scripts npm package, which creates a new package for the WordPress Developmentprovides a set of reusable scripts. This article will detail how to use the ES6,JSX respond in singing @wordpress/scripts contract (to or for)Create a customized Gutenberg lump (of earth)and integrates it into the WooCommerce Center.

图片[1]-如何在 WooCommerce 中创建自定义 Gutenberg 块:详细开发教程

before the beginning (of sth)

Before we start development, we need to configure some necessary development environments:

  • WordPress Development Environment: Make sure you have WordPress installed and running.
  • JavaScript build tools (Node/npm): we will use React respond in singing JSXThe need forConfiguring JavaScript Building Tools.
  • WooCommerce Plugin: If you haven't installed the WooCommerce plugin yet, you need to do so first.

Step 1: Create plugin directory and install dependencies

First of all, it is necessary to set up the wp-content/plugins directory to create aplug-in (software component)catalog and install the required @wordpress/scripts Package.

图片[2]-如何在 WooCommerce 中创建自定义 Gutenberg 块:详细开发教程

The specific steps are as follows:

  1. Creating a plug-in directory: in wp-content/plugins directory to create a file named myFirst-block of the folder.
  2. Installation of dependencies: Enter myFirst-block directory and use the npm mounting @wordpress/scripts Package:
cd wp-content/plugins
mkdir myFirst-block
cd myFirst-block
npm init
npm install --save-dev --save-exact @wordpress/scripts

After the installation is complete, all dependency packages will be saved in the node_modules Catalog.

图片[3]-如何在 WooCommerce 中创建自定义 Gutenberg 块:详细开发教程

Step 2: Configure package.json

exist myFirst-block directory, open the package.json file and add the following script configuration:

"scripts": {
"start": "wp-scripts start",
"build": "wp-scripts build"
}
  • npm run start: Start the development server and put it into listening mode to update the code in real time.
  • npm run build: Packing up the build file and preparing it for release.

Step 3: Create a plugin file to register the Gutenberg block

We need to create a plugin file where we register and load our custom Gutenberg blocks.

图片[4]-如何在 WooCommerce 中创建自定义 Gutenberg 块:详细开发教程

exist myFirst-block directory to create a myFirst-block.php file and add the following code:

<?php
/*
Plugin Name: myFirst-block
Author: webkul
*/
function myFirst_block() {
$styleURI = plugin_dir_url( __FILE__ ) . '/src/style.css' ;
// Enqueue styles
wp_enqueue_style(
'myFirst-block-style'.
$styleURI
);

// Register JavaScript file (build/index.js)
wp_register_script(
'myFirst-block-script'.
plugins_url( 'build/index.js', __FILE__ ),
array( 'wp-blocks', 'wp-element', 'wp-editor' )
);

// Register editor styles
wp_register_style(
'myFirst-block-editor-style'.
plugins_url( 'src/editor.css', __FILE__ )
);

// Register the block
register_block_type( 'myCustomBlock/myFirst-block', array(
'editor_script' => 'myFirst-Block-script',
'editor_style' => 'myFirst-block-editor-style',
) );
}
add_action( 'init', 'myFirst_block' );

The code accomplishes the following:

  • Load custom styles and JavaScript files.
  • Signed up for a new Gutenberg block.

Step 4: Write the JavaScript Code Registration Block

Next, it is necessary to add the myFirst-block/src/index.js file to write the JavaScript code to register our Gutenberg block. First, create the src/index.js file and add the following:

import { registerBlockType } from '@wordpress/blocks' ;
import { RichText } from '@wordpress/block-editor' ;

// Register the first custom block
registerBlockType( 'myCustomBlock/my-first-block', {
title: 'My first customized block',
icon: 'smiley',
category: 'common',
attributes: {
content: {
type: 'string',
source: 'html',
selector: 'p',
},
},
edit: (props) => {
const { attributes: { content }, setAttributes } = props.

const onChangeContent = (newContent) => {
setAttributes({ content: newContent });
};

return (

<RichText
tagName="p"
value={ content }
onChange={ onChangeContent }
placeholder="Please enter content"
/>

);
},
save: (props) => {
return (

<RichText.Content tagName="p" value={props.attributes.content} />

);
}
});

Step 5: Add Style File

图片[5]-如何在 WooCommerce 中创建自定义 Gutenberg 块:详细开发教程

We also need to define the styles for the Gutenberg block. Create two CSS files:src/editor.css for styles in the editor.src/style.css Styles used for front-end presentation.

  1. Editor styles (src/editor.css)::
.my-first-block {
color: blue;
background-color: #f0f0f0f0;
padding: 20px;
}
  1. Front-end style (src/style.css)::
.my-first-block {
color: green;
padding: 20px;
background-color: #e0e0e0;
}

Step 6: Build and Run

When all the files have been created, use the following command to start the development server:

npm run start

This command will add a new command to the src/index.js Automatically builds and updates files as they change build/index.js file. Now your custom Gutenberg block should be ready to use in the editor.

图片[6]-如何在 WooCommerce 中创建自定义 Gutenberg 块:详细开发教程

Step 7: Further functional extensions (optional)

You can also create more complex Gutenberg blocks. For example, we can create a card component block that allows the user to add an image, title, and description. By using the useBlockProps respond in singing InnerBlocks API that allows other blocks to be added inside the card block.

Below is an example of a simple card component block:

import { registerBlockType } from '@wordpress/blocks' ;
import { MediaUpload, RichText, InnerBlocks, useBlockProps } from '@wordpress/block-editor' ;

registerBlockType( 'myCustomBlock/card-block', {
title: 'Card component',
icon: 'grid-view',
category: 'widgets',
attributes: {
title: { type: 'string' },
description: { type: 'string' },
imageUrl: { type: 'string' },
},
edit: (props) => {
const { attributes, setAttributes } = props;
const blockProps = useBlockProps();

return (

<MediaUpload
onSelect={(media) => setAttributes({ imageUrl: media.url })}
render={({ open }) => (

)}
/>
<RichText
tagName="h3"
value={attributes.title}
onChange={(newTitle) => setAttributes({ title: newTitle })}
placeholder="Title"
/>
<RichText
tagName="p"
value={attributes.description}
onChange={(newDescription) => setAttributes({ description: newDescription })}
placeholder="Description"
/>
<InnerBlocks />

);
},
save: (props) => {
const { attributes } = props;
return (

{attributes.title}
<RichText.Content tagName="h3" value={attributes.title} />
<RichText.Content tagName="p" value={attributes.description} />
<InnerBlocks.Content />

);
}
});
图片[7]-如何在 WooCommerce 中创建自定义 Gutenberg 块:详细开发教程

summarize

With the above steps, we created a simple custom Gutenberg block and added theforward part of sth.and editor styles. You can further extend the functionality of the block as needed, such as supporting more elements within the block, adding custom controls, and so on.


Contact Us
Can't read the article? Contact us for free answers! Free help for personal, small business sites!
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
Author: xiesong
THE END
If you like it, support it.
kudos6 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments