Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Wordpress

WordPress: Create Custom Gutenberg Blocks

- 07.07.26 - ErcanOPAK

🧩 Gutenberg = Modern Block Editor

Classic editor is limited. Gutenberg blocks are modern. Custom blocks for any content. User-friendly, flexible, powerful.

📝 Block Registration

// functions.php
function register_custom_blocks() {
    register_block_type(__DIR__ . '/build/block.json');
}
add_action('init', 'register_custom_blocks');

// block.json
{
    "apiVersion": 2,
    "title": "Custom Block",
    "name": "custom/block",
    "category": "design",
    "icon": "smiley",
    "description": "A custom block",
    "supports": {
        "html": false
    },
    "editorScript": "file:./index.js",
    "editorStyle": "file:./index.css",
    "style": "file:./style.css"
}

🎯 Creating a Block

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

registerBlockType('custom/block', {
    edit: ({ attributes, setAttributes }) => {
        const { content } = attributes;
        
        return (
            <RichText
                tagName='div'
                value={content}
                onChange={(content) => setAttributes({ content })}
                placeholder='Write your content...'
            />
        );
    },
    save: ({ attributes }) => {
        const { content } = attributes;
        return <div dangerouslySetInnerHTML={{ __html: content }} />;
    }
});

// With Inspector Controls
import { InspectorControls } from '@wordpress/block-editor';
import { PanelBody, TextControl } from '@wordpress/components';

registerBlockType('custom/block', {
    attributes: {
        content: { type: 'string' },
        title: { type: 'string' },
        color: { type: 'string' }
    },
    edit: ({ attributes, setAttributes }) => {
        const { content, title, color } = attributes;
        
        return (
            <>
                <InspectorControls>
                    <PanelBody title='Settings'>
                        <TextControl
                            label='Title'
                            value={title}
                            onChange={(title) => setAttributes({ title })}
                        />
                        <TextControl
                            label='Color'
                            value={color}
                            onChange={(color) => setAttributes({ color })}
                        />
                    </PanelBody>
                </InspectorControls>
                
                <div style={{ backgroundColor: color }}>
                    <h3>{title}</h3>
                    <RichText
                        tagName='div'
                        value={content}
                        onChange={(content) => setAttributes({ content })}
                        placeholder='Write content...'
                    />
                </div>
            </>
        );
    },
    save: ({ attributes }) => {
        const { content, title, color } = attributes;
        return (
            <div style={{ backgroundColor: color }}>
                <h3>{title}</h3>
                <div dangerouslySetInnerHTML={{ __html: content }} />
            </div>
        );
    }
});

💡 Block Types

  • Static blocks (same content)
  • Dynamic blocks (PHP-rendered)
  • Reusable blocks (shared across posts)
  • Block patterns (pre-made layouts)
  • Block templates (default content)

“Gutenberg blocks modernize WordPress. Custom blocks for any need. Essential for modern WP development.”

— WordPress Developer

Related posts:

WordPress — REST API Auth Failures Behind CDN

WordPress: Essential Security Best Practices

WordPress — Plugins Loading Everywhere Is the Real Killer

Post Views: 1

Post navigation

Photoshop: Master Color Grading for Cinematic Looks
Kubernetes: Use Helm Charts for Package Management

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

July 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jun    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (597)
  • Add Constraint to SQL Table to ensure email contains @ (582)
  • Average of all values in a column that are not zero in SQL (545)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (512)
  • Find numbers with more than two decimal places in SQL (460)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes
  • Ajax: Use Axios for HTTP Requests
  • JavaScript: Understand Hoisting
  • HTML: Use Web Storage for Client-Side Data
  • CSS: Use Filter Effects for Visual Magic
  • Windows 11: Unlock God Mode for All Settings

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com