For many WordPress developers, the hardest part of building a custom block was never the idea. It was the toolchain. Node.js, npm, a build step, React components and a block.json file stood between a perfectly good PHP template and the block inserter. WordPress 7.0 opens a welcoming new path: PHP-only blocks. With a single flag, you can register a fully working block in PHP, and WordPress generates the editor controls for you. In this guide we will explain how PHP-only blocks work, build a practical example step by step, and help you decide when this approach is the right fit.
What Are PHP-Only Blocks?
PHP-only blocks are server-rendered blocks registered entirely with register_block_type() in PHP. They were introduced in WordPress 7.0 through a new block support called autoRegister. When a block declares 'autoRegister' => true inside its supports array and provides a render_callback, WordPress automatically makes the block available in the editor. No JavaScript registration, no build step and no separate block.json file are required.
Behind the scenes, WordPress adds every auto-registered block to a list in the editor settings, and the editor registers those blocks on the client for you. It also reads your attribute definitions and, where possible, builds matching controls in the block sidebar (the Inspector). The block preview you see in the editor is rendered by your PHP callback on the server, so the editor and the front end always show the same markup.
Why this is such welcome news
- Lower barrier to entry. Thousands of experienced PHP developers, theme builders and agency teams can now create blocks with the skills they already have.
- Faster prototyping. An idea can go from a PHP function to a usable block in minutes.
- Lighter plugins. No compiled JavaScript bundle to ship, version or debug for simple blocks.
- Easier modernisation. Many shortcodes and classic widgets can be turned into proper blocks with very little effort.
- One source of truth. Rendering logic lives in PHP, so front-end and editor output stay consistent.
Requirements Before You Start
- WordPress 7.0 or later on the site where the block runs.
- PHP 7.4 or higher, which is the minimum WordPress requires. A current PHP 8 version is strongly recommended; see our guide to the best PHP version for WordPress.
- A plugin or theme to hold the code. A small custom plugin is ideal because your blocks keep working if you change themes.
- A local or staging environment. WordPress Playground and Studio make it easy to spin one up in seconds.
How autoRegister Works: The Minimal Example
Here is the smallest useful PHP-only block. Drop it into a plugin file and activate the plugin:
<?php
/**
* Plugin Name: Hello PHP Block
* Description: A minimal PHP-only block for WordPress 7.0+.
* Version: 1.0.0
*/
add_action( 'init', function () {
register_block_type( 'hello/php-block', array(
'title' => __( 'Hello PHP Block', 'hello' ),
'attributes' => array(
'message' => array(
'label' => __( 'Message', 'hello' ),
'type' => 'string',
'default' => 'Hello from PHP!',
),
),
'render_callback' => function ( $attributes ) {
return '<p>' . esc_html( $attributes['message'] ) . '</p>';
},
'supports' => array(
'autoRegister' => true,
),
) );
} );
Open any post, click the inserter, search for “Hello PHP Block”, and add it. You will see a text control labelled “Message” in the sidebar. Change the text and the preview updates with output from your PHP callback.
Three parts make this work:
- The
autoRegistersupport flag tells WordPress to handle client-side registration automatically. - The
render_callbackis required. PHP-only blocks are dynamic blocks: they have no JavaScript save function, so the server renders the HTML every time. - Attribute definitions with a
labelgive WordPress what it needs to generate sidebar controls with human-friendly, translatable names.

Supported Attribute Types and Generated Controls
The automatically generated controls are intentionally simple in this first version. According to the official dev note, the supported attribute types are:
| Attribute type | Typical use | Notes |
|---|---|---|
string | Headings, labels, button text, URLs | Add an enum list to restrict values to a set of options. |
integer | Counts such as “number of posts” | Whole numbers only. |
number | Decimal values | Useful for ratios or ratings. |
boolean | On/off options | Ideal for “show date” or “open in new tab”. |
A few rules are worth remembering:
- Attributes are stored in the block’s comment delimiter, which is the default for dynamic blocks. Attributes cannot be “sourced” from the saved HTML.
- Attributes of unsupported types, and attributes with the
localrole, do not get a generated control. You can still use them internally. - Each attribute accepts a
label, which you should wrap in__()so it can be translated.
Step-by-Step: Build a “Latest Posts Card” PHP-Only Block
Let us build something closer to a real client request: a block that displays recent posts from a category as simple cards, with options for the number of posts, whether to show the date, and a layout size.
Step 1: Register the block and its attributes
add_action( 'init', 'ak_register_recent_cards_block' );
function ak_register_recent_cards_block() {
register_block_type( 'ak/recent-cards', array(
'title' => __( 'Recent Post Cards', 'ak' ),
'description' => __( 'Shows recent posts as cards.', 'ak' ),
'category' => 'widgets',
'icon' => 'grid-view',
'attributes' => array(
'heading' => array(
'label' => __( 'Heading', 'ak' ),
'type' => 'string',
'default' => 'Latest articles',
),
'count' => array(
'label' => __( 'Number of posts', 'ak' ),
'type' => 'integer',
'default' => 3,
),
'showDate' => array(
'label' => __( 'Show date', 'ak' ),
'type' => 'boolean',
'default' => true,
),
'size' => array(
'label' => __( 'Card size', 'ak' ),
'type' => 'string',
'enum' => array( 'small', 'medium', 'large' ),
'default' => 'medium',
),
),
'render_callback' => 'ak_render_recent_cards_block',
'supports' => array(
'autoRegister' => true,
),
) );
}
Step 2: Write a safe render callback
function ak_render_recent_cards_block( $attributes ) {
$count = max( 1, min( 12, (int) $attributes['count'] ) );
$size = in_array( $attributes['size'], array( 'small', 'medium', 'large' ), true )
? $attributes['size']
: 'medium';
$posts = get_posts( array(
'numberposts' => $count,
'post_status' => 'publish',
'suppress_filters' => false,
) );
if ( empty( $posts ) ) {
return '';
}
$html = '<div ' . get_block_wrapper_attributes( array( 'class' => 'ak-cards ak-cards--' . $size ) ) . '>';
$html .= '<h2>' . esc_html( $attributes['heading'] ) . '</h2><ul>';
foreach ( $posts as $post ) {
$html .= '<li><a href="' . esc_url( get_permalink( $post ) ) . '">'
. esc_html( get_the_title( $post ) ) . '</a>';
if ( ! empty( $attributes['showDate'] ) ) {
$html .= ' <time>' . esc_html( get_the_date( '', $post ) ) . '</time>';
}
$html .= '</li>';
}
return $html . '</ul></div>';
}
Notice the defensive habits: the count is clamped to a sensible range, the enum value is validated again on the server, every piece of output is escaped, and get_block_wrapper_attributes() is used so the block’s wrapper receives the standard block classes.
Step 3: Add styles
For front-end styling, register a stylesheet and pass its handle in the style argument of register_block_type(), or enqueue it conditionally with has_block(). Some early adopters have noted that certain editor-side stylesheet helpers rely on client-side registration, so test your styles inside the editor as well as on the front end.
Step 4: Test in the editor
Insert the block and you will find controls for the heading, the number of posts, the date toggle and the card size. Each change triggers a fresh server render, so the preview always reflects your PHP logic exactly.
Current Limitations to Plan Around
PHP-only blocks are deliberately focused on simple, server-rendered use cases. Knowing the boundaries helps you choose the right tool:
- No inner blocks. A PHP-only block cannot contain other blocks yet.
- No rich text, textarea or media controls. Generated controls cover the simple types listed above. Richer field types may arrive later through ongoing work on a Fields API.
- Server round-trips. Each settings change re-renders on the server, which is fine for most blocks but less suited to highly interactive editing.
- Not a replacement for client-side blocks. Complex editing experiences still benefit from JavaScript blocks.
- Some integrations are still maturing. For example, Block Bindings support for PHP-only blocks was limited in the initial 7.0 release, so check the latest documentation if you need it.
None of these are drawbacks for the job PHP-only blocks were designed for. They are simply signals of when to reach for another approach.
PHP-Only Blocks vs. JavaScript Blocks vs. Shortcodes
| Approach | Best for | Build tools needed | Editor experience |
|---|---|---|---|
| PHP-only block | Dynamic, server-rendered content with simple settings | None | Native block with auto-generated sidebar controls |
| JavaScript (React) block | Rich, interactive editing, inner blocks, inline text | Node.js and a build step (usually) | Fully custom |
| Shortcode | Legacy content and quick embeds | None | Text placeholder only |
A useful rule of thumb: if your block mainly displays data that lives somewhere else (posts, custom fields, an API, settings), start with a PHP-only block. If authors need to type and design content inside the block, a JavaScript block is the better home. And if your block needs front-end interactivity such as tabs or filters, you can pair server rendering with the WordPress Interactivity API.
Great Use Cases for PHP-Only Blocks
- Converting shortcodes such as pricing tables, testimonials lists or contact details into proper blocks.
- Dynamic listings: recent posts, upcoming events, team members, or WooCommerce product highlights.
- Data from custom fields, such as a property’s specifications or a course’s syllabus.
- External data: exchange rates, opening hours, or status badges fetched and cached on the server.
- Agency building blocks: small, reusable components shared across many client sites from one plugin.
Best Practices for Production-Ready PHP-Only Blocks
Escape and validate everything
Attributes come from the post content, so treat them as user input. Use esc_html(), esc_attr() and esc_url() on output, and validate numbers and enum values in your render callback even though the editor already restricts them.
Keep render callbacks fast
Because the callback runs on every page view, cache expensive work. Store remote API results in transients and keep database queries lean. A persistent object cache helps a lot here; our Redis object cache guide covers setup and tuning.
Use namespaces and clear labels
Prefix block names with a unique namespace (for example ak/recent-cards) and write clear, translatable labels. Your editors will thank you, and translation plugins can localise the interface.
Support older WordPress versions gracefully
If your plugin also runs on versions older than 7.0, the autoRegister flag will simply be ignored there and the block will not appear in the inserter. Document the 7.0 requirement in your plugin header with Requires at least: 7.0, or provide a shortcode fallback that calls the same render function.
Think about accessibility from the start
Use semantic HTML such as lists, headings and time elements, ensure links have descriptive text, and check colour contrast in your styles. Our WCAG 2.2 checklist is a handy reference.
Migrating a Shortcode to a PHP-Only Block
If you already have a shortcode, you are most of the way there. A smooth migration usually looks like this:
- Move the shortcode’s output logic into a standalone render function that accepts an array of settings.
- Register a PHP-only block whose attributes mirror the shortcode’s parameters, and point its
render_callbackat that function. - Keep the shortcode registered and have it call the same function, so existing content keeps working.
- Encourage editors to use the block for new content, and convert old pages gradually.
This gives you one rendering function, two entry points, and a gentle upgrade path with no broken pages.
Organising a Library of PHP-Only Blocks
Once your first block works, it is tempting to add the next one to the same file. That is fine for two or three blocks, but a little structure pays off quickly as your collection grows. A clean layout that many teams use looks like this:
my-blocks/
├── my-blocks.php (plugin header + loader)
├── blocks/
│ ├── recent-cards.php (registration + render callback)
│ ├── team-member.php
│ └── opening-hours.php
└── assets/
└── blocks.css
The main plugin file simply loops through the blocks folder and includes each file. Every block file contains one registration function and one render function, so it is easy to find, review and reuse. Keeping one block per file also makes code reviews simpler and lets you copy a single block into another project without untangling dependencies.
Share helpers between blocks
Many blocks need the same small utilities: clamping numbers, validating enum values, building a card from a post, or reading a cached API response. Put those into a shared helpers file. Your render callbacks become shorter and more consistent, and a bug fixed once is fixed everywhere.
Version and document each block
Add a short comment above each registration describing what the block does, which attributes it accepts and any data it depends on. Because attributes are stored in the post content, changing an attribute name later affects existing posts. Prefer adding new attributes with sensible defaults over renaming old ones, and your saved content will keep working smoothly after every update.
Debugging Tips When a Block Does Not Appear
If your new block does not show up in the inserter, work through this quick checklist:
- Confirm the WordPress version. The
autoRegistersupport is available from WordPress 7.0 onwards. - Check the hook. Registration should run on
init, not later. - Verify the block name. It must use the
namespace/block-nameformat with lowercase letters, numbers and dashes. - Make sure a
render_callbackexists. Without it, the block is not eligible for auto-registration. - Look for PHP notices. Enable
WP_DEBUGandWP_DEBUG_LOGon your development site and checkwp-content/debug.log. - Inspect the editor settings. In the browser console, the editor settings include the list of auto-registered blocks, which quickly confirms whether WordPress received your block.
If the block appears but the preview looks empty, test your render callback directly by calling it with an array of default attributes. Most preview issues come from a missing default value or an early return '' when no posts or data are found. Returning a short, friendly placeholder message in the editor context can make these situations much clearer for authors.
Frequently Asked Questions
Do PHP-only blocks need a block.json file?
No. You can define the block’s metadata directly in the register_block_type() call. Using a block.json file is still possible if you prefer keeping metadata in a separate file.
Can PHP-only blocks use design tools like colour and spacing?
The block supports system is shared by all blocks, and early testers report that common supports work alongside autoRegister. The official dev note focuses on attributes, so test any specific support you rely on and check the Block Editor Handbook for updates.
Are PHP-only blocks good for performance?
They ship no block-specific JavaScript to visitors, which is great for front-end performance. Keep the render callback efficient and cached, and they are an excellent choice for fast sites. If you are tuning responsiveness, our guide to INP optimization is a useful companion.
Conclusion
PHP-only blocks are one of the most developer-friendly additions in WordPress 7.0. With the autoRegister flag and a render callback, any PHP developer can turn templates, shortcodes and data-driven components into native blocks with real editor controls, without a JavaScript build step. Start with one small block, follow the escaping and caching habits above, and you will quickly build a library of fast, maintainable blocks that your clients and editors will enjoy using.
References and Further Reading
- PHP-only block registration – Make WordPress Core dev note
- Registration of a block – Block Editor Handbook
- register_block_type() – WordPress Developer Reference
- get_block_wrapper_attributes() – WordPress Developer Reference
Images: Ilya Pavlov and Chris Ried on Unsplash.

Leave a Reply