Validation API Core Blocks
Registers Validation API checks for WordPress core blocks.
by Troy Chaplin · github.com/troychaplin/validation-api-core-blocks
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/troychaplin/validation-api-core-blocks/archive/refs/heads/main.zipReadme
Validation API Core Blocks
Registers Validation API checks for WordPress core blocks. All checks are block-level — no custom post types, no meta fields, no custom blocks.
Requires: The Validation API plugin must be active. If it is not, this plugin loads silently without registering any checks.
Checks
core/image
| Check | Default | Description |
|---|---|---|
check_image_alt_text |
error | Alt text is required once an image is selected. Decorative images (isDecorative) are exempt. |
check_image_alt_text_length |
error | Alt text must not exceed 125 characters. |
check_image_alt_caption_match |
error | Alt text and caption must not be identical. |
check_image_alt_text_patterns |
error | Alt text must not be a filename, a single generic word (image, photo, pic, …), or a short phrase starting with image of, photo of, etc. |
core/button
| Check | Default | Description |
|---|---|---|
check_button_link |
error | A valid URL is required. <button> elements (no navigation intent) are exempt. |
check_button_text |
error | Button text must be present and contain at least one alphanumeric character. |
core/table
| Check | Default | Description |
|---|---|---|
check_table_headers |
error | The table must have a header section (<thead>) or header cells (<th>) in the first row. |
core/heading
| Check | Default | Description |
|---|---|---|
check_heading_rank |
error | Heading levels must not skip ranks (e.g. H2 → H4). The first heading in the document must be H1 or H2. |
Severity for any check can be overridden at runtime without touching this plugin's code via the validation_api_check_level filter provided by the Validation API.
File Structure
validation-api-core-blocks/
├── Functions/
│ ├── Check_Core_Button.php # Registers core/button checks
│ ├── Check_Core_Heading.php # Registers core/heading checks
│ ├── Check_Core_Image.php # Registers core/image checks
│ ├── Check_Core_Table.php # Registers core/table checks
│ ├── Enqueues.php # Enqueues editor-script.js
│ └── Plugin_Paths.php # Plugin URL / path helpers
├── src/
│ ├── editor-script.js # Entry point — imports all check files
│ └── scripts/
│ ├── checks/
│ │ ├── core-button.js # editor.validateBlock logic for core/button
│ │ ├── core-heading.js # editor.validateBlock logic for core/heading
│ │ ├── core-image.js # editor.validateBlock logic for core/image
│ │ ├── core-table.js # editor.validateBlock logic for core/table
│ │ └── editor-checks.js # editor.validateEditor scaffold (empty)
│ └── utils/
│ └── isValidUrl.js # Lightweight URL validator
├── plugin.php
└── README.md
Adding a New Check
1. Register the check in PHP
Create Functions/Check_Core_Paragraph.php (or add to an existing class):
namespace Validation_API_Core_Blocks;
class Check_Core_Paragraph {
public function __construct() {
add_action( 'init', array( $this, 'register_checks' ) );
}
public function register_checks() {
if ( ! function_exists( 'validation_api_register_block_check' ) ) {
return;
}
validation_api_register_block_check(
'core/paragraph',
array(
'namespace' => 'validation-api-core-blocks',
'name' => 'check_paragraph_not_empty',
'level' => 'error',
'error_msg' => __( 'Paragraph blocks must not be empty.', 'validation-api-core-blocks' ),
'warning_msg' => __( 'Consider removing empty paragraphs.', 'validation-api-core-blocks' ),
'description' => __( 'Paragraph blocks require content.', 'validation-api-core-blocks' ),
)
);
}
}
Instantiate it in plugin.php:
\Validation_API_Core_Blocks\Check_Core_Paragraph::class,
2. Implement the validation logic in JavaScript
Create src/scripts/checks/core-paragraph.js:
import { addFilter } from '@wordpress/hooks';
addFilter(
'editor.validateBlock',
'validation-api-core-blocks/paragraphValidation',
(isValid, blockType, attributes, checkName) => {
if (blockType !== 'core/paragraph') {
return isValid;
}
switch (checkName) {
case 'check_paragraph_not_empty':
return !!(attributes.content && attributes.content.trim());
default:
return isValid;
}
}
);
Import it in src/editor-script.js:
import './scripts/checks/core-paragraph.js';
Development
# Install dependencies
npm install
composer install
# Development build with watch mode
npm run start
# Production build
npm run build
# Lint and format
npm run lint
npm run format