WordPress 6.4 for developers: Block Hooks, admin notices and options
WordPress 6.4 (November 2023) is a release that looks small for users: a new default theme Twenty Twenty-Four, a lightbox for images, pattern categories. For developers it brought a few APIs that replace old workarounds: Block Hooks to insert a block into themes, two functions for admin notices, faster loading of options and a switch that turns off attachment pages.
Problem: A plugin adds a “Like” button below the post content. In classic themes the the_content filter did that; in block themes the button had to be placed manually by every site owner. The same plugin builds its admin notices from copied HTML and loads its twelve options with twelve queries.
Solution: Use the Block Hooks API (the blockHooks field in block.json) to insert the plugin’s block automatically next to a core block, print notices with wp_admin_notice(), and load several options at once with wp_prime_option_caches(), all available since WordPress 6.4.
Block Hooks
A block declares where it wants to be inserted, relative to another block type:
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "ha/like-button",
"title": "Like button",
"category": "widgets",
"blockHooks": {
"core/post-content": "after"
},
"render": "file:./render.php"
}
The positions are before, after, firstChild and lastChild. WordPress inserts the block when it renders templates, template parts and patterns of a block theme. In 6.4 this works only for templates the user has not edited in the Site Editor; WordPress 6.5 extended it to edited templates, and a hooked block can then be moved or removed there like any other block.
For conditions that block.json cannot express, the hooked_block_types filter adds or removes hooked blocks in PHP:
add_filter( 'hooked_block_types', 'ha_hook_like_button', 10, 4 );
function ha_hook_like_button( array $hooked, string $position, string $anchor, $context ): array {
// Only after the content in the single post template, not on pages.
if ( 'core/post-content' === $anchor && 'after' === $position
&& $context instanceof WP_Block_Template && 'single' !== $context->slug ) {
$hooked = array_diff( $hooked, array( 'ha/like-button' ) );
}
return $hooked;
}
wp_admin_notice()
add_action( 'admin_notices', 'ha_settings_notice' );
function ha_settings_notice(): void {
if ( get_option( 'ha_api_key' ) ) {
return;
}
wp_admin_notice(
sprintf(
/* translators: %s: settings page URL */
__( 'Enter your API key on the <a href="%s">settings page</a>.', 'textdomain' ),
esc_url( admin_url( 'options-general.php?page=ha-settings' ) )
),
array(
'type' => 'warning',
'dismissible' => true,
)
);
}
wp_admin_notice() prints the markup that core uses (the notice notice-warning is-dismissible classes) and runs the message through wp_kses_post(), so links are allowed and scripts are not. wp_get_admin_notice() returns the same HTML as a string. The types are success, info, warning and error.
Options in one query
// One query for all options that are not autoloaded, instead of one query per get_option().
wp_prime_option_caches( array( 'ha_api_key', 'ha_cache_ttl', 'ha_last_sync', 'ha_mapping' ) );
$ttl = (int) get_option( 'ha_cache_ttl', 3600 ); // from the cache now
// Large options that are only needed on the settings page: do not autoload them.
wp_set_option_autoload_values( array(
'ha_mapping' => false,
'ha_last_log' => false,
) );
Autoloaded options are loaded on every request, including the front end, so a large option that only one admin screen needs should not be autoloaded. Since 6.4 that can be changed for existing options without deleting and re-adding them. Why the autoloaded size matters: wp_options autoload.
Attachment pages off for new sites
New installations of 6.4 redirect attachment pages to the file itself; the setting is the option wp_attachment_pages_enabled. Updated sites keep their attachment pages (the option is 1) until someone sets it to 0, for example with wp option update wp_attachment_pages_enabled 0. Those thin pages have no value in search results, so the change is worth making on most sites.
NOTE: Code that uses these functions needs Requires at least: 6.4 in the plugin header, or a function_exists() check with the old way as fallback. The next release, WordPress 6.5 (April 2024), followed with the Block Bindings API (block attributes from custom fields), the Interactivity API and the Font Library. How block themes are structured in the first place: WordPress 5.9 Full Site Editing.