WP Manifestindependent plugin directory
manifest / editor / wp7-php-block-registration-demo

WP7 PHP Block Demo

A WordPress plugin to demonstrate the PHP-only registration of a block with settings that can be modified in the WordPress sidebar. This ability has been made available only as of WordPress v7.0.

by Gary Smith · github.com/garyesmith/wp7-php-block-registration-demo

0stars
0forks

Install

No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:

wp plugin install https://github.com/garyesmith/wp7-php-block-registration-demo/archive/refs/heads/main.zip

Readme

WordPress 7 PHP-Only Block Registration Demo

This plugin demonstrates a simple example of PHP-Only Block Registration to create a WordPress block that can be customized via settings in the WP admin sidebar. This ability to do this purely with PHP has been available only as of WordPress v7.0. The code here is modified and extended from examples provided by Brian Coords. This post by Ciprian Popescu is another excellent resource.

The demo block is a straightforward category posts block that retrieves some posts from a specified category, then displays their titles and thumbnails horizontally or vertically.

A live demo of the front-end interface for this block is available at https://wpdemo.garysmith.ca.

Sample Block Admin

Screen grab of Block admin view

Sample Block Front-End

Screen grab of Block front end view

Installation Instructions

  • Download this repo as a .zip file and install it as a plugin on any site running WordPress 7.0 or newer.

  • Activate the plugin WP7 PHP Block Demo.

  • Publish some posts and assign them to some categories; make sure each post has a featured image.

  • Edit a page and add the block named Simple PHP Block Demo.

  • With the block selected, in the right sidebar, adjust the block settings as desired:

    • Heading Text - Text to display in the heading
    • Category - Select category to pull posts from
    • Default Direction - Select vertical or horizontal
    • Max Posts - Select 1, 2, or 3 posts
  • With the block selected, in the right sidebar, adjust any default style settings such as font sizes or background colors.

  • Publish the post, then view it.

  • When viewing, click the Reverse Order button at the top-right of any block instance to change the display direction of the posts dynamically.

Code Structure Notes

The file php-blocks-demo.php in the root defines this as a WP plugin, sets some path constants, then loads the block registration code.

The block code is contained in the subfolder simple-block-demo which contains 3 files:

  • register.php - Registers the block, enqueues the CSS and Javascript, defines the properties of the block including the attributes available in the settings sidebar, then renders the block HTML based on those settings.

  • style.css - CSS to style the block on the front and back-end.

  • view.js - JavaScript that runs on the front-end after the DOM and blocks have loaded, and then injects a button that enables the display direction of the posts in the block to be changed on-the-fly with a simple JavaScript sort. Important note: This JavaScript will only execute when the block is viewed on the front-end of the website; it will not execute within the WordPress admin panel content editing area.

Inline code comments provide further implementation details.

Note that any number of blocks could be registered within one plugin, with each contained in its own subfolder.

What is PHP-Only Block Registration?

Until the release of WordPress 7.0 in early 2026, the creation of all custom WordPress blocks (formerly known as Gutenberg blocks) required working with a Javascript build environment that includes NodeJS, npm, React, and other related build tools. Since the rest of WordPress is built and customized via PHP, this made custom block development cumbersome, bringing along a lot of overhead even for developers who work in both stacks.

PHP-Only Block Registration in WordPress 7.0 eliminates the need for this secondary stack when creating simple server-rendered blocks (see below). Specifically, a new autoRegister flag in register_block_type instructs WordPress to automatically activate the block in the editor, along with the relevant sidebar setting elements.

Limitations of PHP-Only Block Registration

This method is designed to ease block registration for simpler blocks that do not have any of the following requirements:

  • The need for nested blocks, that is, the ability to drop another block into your custom block.

  • Sidebar attribute setting types other than string, integer, boolean, and enum.

  • Advanced media fields like image uploaders or rich text fields.

  • Extensive user interaction with a lot of JavaScript that must execute in the WordPress admin area.

Even with these limitatations, this change should open up new customization possibilities for more WordPress developers.

Read the full README on GitHub →