Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 37 of the Intermediate WordPress Plugins: REST API & React Admin course
WordPressJune 26, 20264 min read

Integrating with Gutenberg Blocks: Dynamic Data Fetching

Learn to build dynamic Gutenberg blocks that fetch live Knowledge Base data via the REST API. Master the @wordpress/data store integration for your blocks.

GutenbergREST APIReactWordPressBlocksDynamic Blocksphpplugin-development

Previously in this course, we built a robust custom hook for API logic to streamline our React admin dashboard. Now, we're taking that knowledge into the content editor. In this lesson, we will build a dynamic block that fetches and displays our Knowledge Base entries directly inside the Gutenberg editor, allowing users to select and embed content without leaving the post-editing screen.

Understanding Dynamic Blocks

A dynamic block is a block that renders its output on the server side using PHP, rather than just saving static HTML to the post_content. This is crucial for our Knowledge Base plugin because if we update a KB entry in our admin dashboard, we want that change to reflect immediately across all posts where the block is used.

Instead of saving the rendered HTML, we save a "placeholder" (the block attributes) and let the render_callback handle the database query and output generation.

Integrating with Gutenberg Blocks via the REST API

To make this work, we need to bridge our existing API infrastructure with the Block Editor's data store. We will use the @wordpress/data package, which we explored when registering a custom data store, to fetch our KB entries.

1. Registering the Dynamic Block

First, we define our block in PHP. We need to register the block type and provide a render_callback function.

PHP
#6A9955">// kb-plugin/blocks/kb-display/index.php
register_block_type( 'kb-plugin/kb-display', array(
    'attributes' => array(
        'kbId' => array(
            'type' => 'integer',
        ),
    ),
    'render_callback' => 'render_kb_display_block',
) );

function render_kb_display_block( $attributes ) {
    if ( empty( $attributes['kbId'] ) ) {
        return '<p>Please select a Knowledge Base entry.</p>';
    }
    
    #6A9955">// Fetch the specific KB entry using our established service layer
    $post = get_post( $attributes['kbId'] );
    return sprintf( '<div class="kb-entry"><h2>%s</h2>%s</div>', 
        esc_html( $post->post_title ), 
        apply_filters( 'the_content', $post->post_content ) 
    );
}

2. Fetching Data in the Editor

In our JavaScript block code, we need to display a dropdown or list of available KB entries. We'll use useSelect to query our custom data store.

JAVASCRIPT
// kb-plugin/src/blocks/kb-display/edit.js
import { useSelect } from CE9178">'@wordpress/data';
import { SelectControl } from CE9178">'@wordpress/components';

export default function Edit( { attributes, setAttributes } ) {
    const { kbId } = attributes;

    const { kbEntries, isResolving } = useSelect( ( select ) => {
        const store = select( CE9178">'kb-plugin/data' );
        return {
            kbEntries: store.getEntries(),
            isResolving: store.isResolving( CE9178">'getEntries' ),
        };
    }, [] );

    if ( isResolving ) return <p>Loading entries...</p>;

    return (
        <SelectControl
            label="Select KB Entry"
            value={ kbId }
            options={ [
                { label: CE9178">'Select one...', value: 0 },
                ...kbEntries.map( ( e ) => ( { label: e.title.rendered, value: e.id } ) ),
            ] }
            onChange={ ( val ) => setAttributes( { kbId: parseInt( val ) } ) }
        />
    );
}

Hands-on Exercise

  1. Create a new directory blocks/kb-display in your plugin.
  2. Register the block in your main plugin file using register_block_type.
  3. Implement the edit.js file provided above.
  4. Ensure your package.json is configured to build the new block assets using @wordpress/scripts.
  5. Run npm run build and verify that the block appears in the editor and successfully populates the SelectControl with your KB entries.

Common Pitfalls

  • Forgetting to define attributes: If you don't define your kbId attribute in the PHP registration, the editor won't know how to save the block state to the database.
  • Mixing up client/server logic: Remember that the edit.js runs in the browser, while the render_callback runs on the server. Do not try to call get_posts() inside your edit.js file; always use the REST API via your store.
  • Stale Data: Since we are using our custom data store, ensure your resolvers are correctly handling the cache. If you've been optimizing API response times, ensure that invalidating the cache doesn't break the block's ability to fetch the initial list.

Recap

We've successfully bridged the gap between our React admin dashboard and the Gutenberg editor. By creating a dynamic block, we keep our content presentation decoupled from the block's storage. We've utilized our existing REST API and data store to power the block's UI, ensuring a consistent developer experience across the entire plugin.

Up next: We will address potential data conflicts by implementing optimistic locking to ensure that multiple editors don't overwrite each other's changes.

Previous lessonCreating Custom Hooks for API LogicNext lesson Handling Conflict Resolution
Back to Blog

Similar Posts

WordPressJune 26, 20263 min read

Implementing Activity Logging: Auditing REST API Changes in WordPress

Learn to build a robust audit system by hooking into the WordPress REST API, logging changes to a custom table, and displaying the history in your React UI.

Read more
WordPressJune 26, 20263 min read

Building a Modal Confirmation System for WordPress Plugins

Learn to build a safe, user-centric Modal confirmation system in your WordPress React admin UI to prevent accidental data loss during API operations.

Part of the course

Intermediate WordPress Plugins: REST API & React Admin

intermediate · Lesson 37 of 45

  1. 1

    Setting up the WordPress Development Environment

    3 min
  2. 2

    Introduction to @wordpress/scripts

    3 min
  3. 3

    Configuring ESLint and Prettier

    3 min
Read more
WordPressJune 26, 20264 min read

Handling Conflict Resolution: Optimistic Locking in WordPress

Prevent data overwrites and ensure data integrity in your WordPress plugins by implementing optimistic locking, handling concurrency, and improving UX.

Read more
4

Localizing Data for JavaScript

3 min
  • 5

    Anatomy of a REST API Endpoint

    3 min
  • 6

    Implementing REST API Permission Callbacks

    3 min
  • 7

    Handling GET Requests in REST API

    3 min
  • 8

    Validating and Sanitizing API Arguments

    4 min
  • 9

    Creating POST Endpoints for Data Submission

    3 min
  • 10

    Updating Existing API Resources

    3 min
  • 11

    Handling Asynchronous State in React

    3 min
  • 12

    Building the Knowledge Base Service Layer

    3 min
  • 13

    Scaffolding the React Admin Dashboard

    3 min
  • 14

    Working with @wordpress/components

    3 min
  • 15

    Creating a React Form for Submissions

    3 min
  • 16

    Implementing CRUD in the Admin UI

    3 min
  • 17

    Understanding WordPress Data Store Architecture

    4 min
  • 18

    Registering a Custom Data Store

    3 min
  • 19

    Writing Selectors for Data Access

    3 min
  • 20

    Defining Actions and Reducers

    3 min
  • 21

    Implementing Resolvers for Data Fetching

    3 min
  • 22

    Optimizing Performance with Selectors

    3 min
  • 23

    Handling Complex State Dependencies

    4 min
  • 24

    Implementing Nonce Verification

    4 min
  • 25

    Advanced Sanitization Techniques

    3 min
  • 26

    Input Validation and Error Handling

    3 min
  • 27

    Protecting Admin Screens

    3 min
  • 28

    Production Build Pipeline

    3 min
  • 29

    Debugging React in the WordPress Admin

    4 min
  • 30

    Building Search and Filter Functionality

    3 min
  • 31

    Internationalization in React

    3 min
  • 32

    Managing File Uploads via REST API

    3 min
  • 33

    Optimizing API Response Times

    3 min
  • 34

    Working with Date and Time in React

    3 min
  • 35

    Implementing Drag-and-Drop Sorting

    3 min
  • 36

    Creating Custom Hooks for API Logic

    3 min
  • 37

    Integrating with Gutenberg Blocks

    4 min
  • 38

    Handling Conflict Resolution

    4 min
  • 39

    Building a Modal Confirmation System

    3 min
  • 40

    Implementing Activity Logging

    3 min
  • 41

    Using Webpack Aliases

    3 min
  • 42

    Unit Testing API Endpoints

    3 min
  • 43

    Unit Testing React Components

    Coming soon
  • 44

    Handling Large Datasets with GraphQL

    Coming soon
  • 45

    Implementing Real-time Updates with Web

    Coming soon
  • View full course