# Block Publishing Until Every Image Has Alt Text

> Disables the Publish and Update buttons in the block editor while any image block is missing alternative text, and shows a notice explaining why.

- Type: PHP
- Runs: Admin only
- Category: [Accessibility](https://wpide.com/library/category/accessibility/)
- Tags: alt-text, block-editor, publishing
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/a11y-lock-publish-without-alt/

## Code

```php
add_action( 'enqueue_block_editor_assets', function () {
    wp_register_script( 'wpide-alt-lock', '', array( 'wp-data', 'wp-editor', 'wp-notices' ), null, true );
    wp_enqueue_script( 'wpide-alt-lock' );
    wp_add_inline_script( 'wpide-alt-lock', <<<'JS'
(function (wp) {
    var locked = false;
    function missingAlt(blocks) {
        return blocks.some(function (b) {
            var noAlt = b.name === 'core/image' && b.attributes.url && !(b.attributes.alt || '').trim();
            return noAlt || (b.innerBlocks && missingAlt(b.innerBlocks));
        });
    }
    wp.data.subscribe(function () {
        var blocks = wp.data.select('core/block-editor').getBlocks();
        var missing = missingAlt(blocks);
        if (missing === locked) { return; }
        locked = missing;
        var editor = wp.data.dispatch('core/editor');
        var notices = wp.data.dispatch('core/notices');
        if (missing) {
            editor.lockPostSaving('wpide-alt');
            notices.createNotice('warning', 'Add alternative text to every image before publishing.', { id: 'wpide-alt', isDismissible: false });
        } else {
            editor.unlockPostSaving('wpide-alt');
            notices.removeNotice('wpide-alt');
        }
    });
})(window.wp);
JS
    );
} );
```

## Notes

Strict by design; for decorative images type a short description anyway or use the pre-publish warning snippet instead.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Block Publishing Until Every Image Has Alt Text" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
