A Beginner's Guide to Creating Custom Gutenberg Blocks in WordPress

Hire Now!

Whether you’re looking to launch your brand, showcase your portfolio, or open an online store, we’re here to bring your ideas to life.

  • Post Created: 7 days ago
  • Views: 2

A Beginner's Guide to Creating Custom Gutenberg Blocks in WordPress

Reading Time: 4 Minutes

A Beginner's Guide to Creating Custom Gutenberg Blocks in WordPress

The Gutenberg editor, introduced in WordPress 5.0, revolutionized how content is created and managed. Its block-based structure allows users to design complex layouts with ease. While the editor includes a variety of default blocks, creating custom Gutenberg blocks enables you to meet specific design and functionality requirements.

This guide will walk you through the process of creating custom Gutenberg blocks for your WordPress site.


What Are Gutenberg Blocks?

Gutenberg blocks are modular elements that represent different types of content, such as paragraphs, images, or galleries. Custom blocks let you extend this functionality to include unique components tailored to your needs.

Benefits of Custom Blocks

  1. Flexibility: Tailor blocks to specific design and content requirements.
  2. Reusability: Save and reuse custom blocks across multiple pages.
  3. Consistency: Maintain uniformity in layouts and styles.

Prerequisites for Creating Custom Blocks

  1. Basic Knowledge:
    • Familiarity with JavaScript, React, and PHP.
  2. Development Tools:
    • A code editor (e.g., Visual Studio Code).
    • Node.js and npm installed on your system.
  3. Setup a Development Environment:
    • Use a local development environment like Local by Flywheel or XAMPP.

Step 1: Set Up Your Block Plugin

1. Create a Plugin Folder

  1. Navigate to wp-content/plugins/.
  2. Create a new folder, e.g., custom-gutenberg-blocks.

2. Create a Plugin File

1- Inside your folder, create a file named custom-gutenberg-blocks.php.
2- Add the following header:

<?php /* Plugin Name: Custom Gutenberg Blocks Description: A plugin to create custom Gutenberg blocks. Version: 1.0 Author: Your Name */ ?>

3- Activate the plugin from the WordPress dashboard.


Step 2: Register Your Custom Block

1. Add a Block Registration Script

Create a block.js file in your plugin folder to register the block.

2. Enqueue Block Scripts

In your custom-gutenberg-blocks.php file, enqueue the block scripts:

function custom_gutenberg_block_scripts() {
    wp_enqueue_script(
        'custom-block-js',
        plugins_url('block.js', __FILE__),
        array('wp-blocks', 'wp-element', 'wp-editor'),
        '1.0',
        true
    );
}
add_action('enqueue_block_editor_assets', 'custom_gutenberg_block_scripts');

Step 3: Write Your Block Code

1. Basic Block Code in block.js

const { registerBlockType } = wp.blocks;
const { RichText } = wp.blockEditor;

registerBlockType('custom/block', {
    title: 'Custom Block',
    icon: 'smiley',
    category: 'common',
    attributes: {
        content: { type: 'string', source: 'html', selector: 'p' }
    },
    edit: ({ attributes, setAttributes }) => {
        const onChangeContent = (content) => {
            setAttributes({ content });
        };

        return (
            <RichText
                tagName="p"
                value={attributes.content}
                onChange={onChangeContent}
                placeholder="Enter text here..."
            />
        );
    },
    save: ({ attributes }) => {
        return <RichText.Content tagName="p" value={attributes.content} />;
    }
});

2. Explanation

  • Attributes: Define the data your block will use.
  • Edit: Handles the block’s appearance in the editor.
  • Save: Defines how the block content is rendered on the frontend.

Step 4: Add Styling

1. Create a CSS File

Add a style.css file to your plugin folder for frontend styles:

.custom-block {
    background-color: #f9f9f9;
    padding: 20px;
    border: 1px solid #ddd;
}

2. Enqueue Styles

Enqueue your CSS file in custom-gutenberg-blocks.php:

function custom_gutenberg_block_styles() {
    wp_enqueue_style(
        'custom-block-css',
        plugins_url('style.css', __FILE__),
        array(),
        '1.0'
    );
}
add_action('enqueue_block_assets', 'custom_gutenberg_block_styles');

Step 5: Test Your Block

  1. Go to the WordPress editor.
  2. Search for your custom block by its title.
  3. Add the block to a post or page.
  4. Test its functionality and appearance.

Best Practices for Custom Gutenberg Blocks

  1. Keep Code Modular
    • Split JavaScript files for complex blocks.
  2. Follow Accessibility Standards
    • Ensure your block is accessible for all users.
  3. Use WordPress Development Standards
    • Follow coding standards for PHP, JavaScript, and CSS.
  4. Optimize Performance
    • Minify JavaScript and CSS files for production.

Recommended Tools

  1. @wordpress/scripts
    • Simplifies block development with preconfigured tools.
  2. Gutenberg Handbook
    • Official WordPress documentation for block development.
  3. Block Lab Plugin
    • Create custom blocks with a GUI.

Conclusion

Custom Gutenberg blocks enable you to create tailored content elements that enhance your WordPress site’s functionality and design. By following this guide, you can build blocks that meet your unique requirements and provide an improved editing experience.

Start creating your custom Gutenberg blocks today to unlock the full potential of the WordPress editor.

Discussion