Independent Editorial DeskWordPress Releases, Builds, and Operations
Back to Archive
Implementation Notes

WordPress has_block Example: Detect Blocks Before Loading Assets

Use has_block() to load block-specific CSS or behavior only when the current content actually contains that block.

Published

April 29, 2026

Reading Time

2 min read

Updated

April 29, 2026

Abstract content scan representing conditional asset loading for WordPress blocks.
Build PatternImplementation Notes

Implementation Notes

Extension points, code paths, and implementation choices that should survive contact with production.

Best For

WordPress developers, agencies, and technical teams building custom plugin or theme functionality with cleaner operational defaults.

Primary Topics

Implementation Notes

Editorial Focus

Build Pattern: Extension points, code paths, and implementation choices that should survive contact with production. Updated on April 29, 2026.

Full Report

Last reviewed: April 29, 2026

Many themes and plugins load front-end assets on every page even when only one block actually needs them. has_block() is a practical way to detect block presence and keep scripts, styles, or conditional UI logic aligned with the content that is really on the page.

This guide shows how to use has_block() to load assets only when a given block exists in the current post content.

Conditionally enqueue block-specific CSS

<?php
add_action( 'wp_enqueue_scripts', 'vulnwp_enqueue_gallery_assets' );

function vulnwp_enqueue_gallery_assets() {
	if ( ! is_singular() ) {
		return;
	}

	if ( has_block( 'core/gallery', get_queried_object_id() ) ) {
		wp_enqueue_style(
			'vulnwp-gallery-enhancements',
			get_stylesheet_directory_uri() . '/assets/gallery.css',
			array(),
			null
		);
	}
}

This keeps archive pages and unrelated content from paying for gallery-specific CSS they never use.

Use the canonical block name

The function expects the full block name such as core/gallery or acf/faq. Passing a vague or shortened identifier makes detection fail quietly.

Prefer it for content-aware decisions, not permissions or business rules

has_block() is useful when the rendered content determines whether an asset or wrapper belongs on the page. It should not replace capability checks, route checks, or more formal feature toggles.

Keep the condition close to the asset or behavior it controls

Scattering unrelated block checks across the theme makes it harder to debug later. The block detection should live near the code it enables.

Production checklist

  • Use the full block name in every check.
  • Run the logic only in contexts where post content exists.
  • Keep block-aware enqueues targeted to the asset they gate.
  • Test pages where the block is absent, present once, and present multiple times.
  • Do not confuse content detection with authorization or feature flags.
  • Review conditional loads in performance testing after rollout.

Common mistakes

  • Using a shortened block identifier. The check expects the full namespace/name format.
  • Running block detection where no singular content is loaded. The condition becomes unreliable.
  • Loading assets globally anyway. That defeats the purpose of the check.
  • Turning content detection into business logic. Block presence is not an access-control mechanism.
  • Forgetting custom blocks. Third-party or ACF blocks need their real registered names.

Related reading

If the block assets are registered through theme or plugin script loading, pair this with the wp_enqueue_script guide. If the block itself is custom, the block.json article is the next step.

References and further reading

Popular Guides

Popular WordPress guides to read next.

These articles connect recurring production concerns: implementation details, updates, troubleshooting, recovery paths, and operational cleanup.

Continue Reading

More from the archive.

Diagnostic dashboard scene representing a WordPress Site Health review before major updates.
01Build Pattern
Implementation Notes

Build Pattern

Extension points, code paths, and implementation choices that should survive contact with production.

May 21, 2026 · 3 min read

WordPress Site Health Check Before Major Updates: What to Review First

A pre-update WordPress Site Health checklist covering loopbacks, connectivity, debug settings, and environment readiness.

Structured data and route review scene representing permalink validation after a WordPress migration.
02Build Pattern
Implementation Notes

Build Pattern

Extension points, code paths, and implementation choices that should survive contact with production.

May 21, 2026 · 3 min read

WordPress Permalink Checklist After Migration: Catch URL Problems Early

A post-migration WordPress permalink checklist for checking rewrite rules, post URLs, archives, and redirect noise.

Technical media workspace representing image preparation and optimization before upload to WordPress.
03Build Pattern
Implementation Notes

Build Pattern

Extension points, code paths, and implementation choices that should survive contact with production.

May 21, 2026 · 3 min read

WordPress Image Optimization Checklist: What to Fix Before Upload

A practical WordPress image optimization checklist covering dimensions, compression, formats, and Media settings before upload.