# Accessibility Check When a Post Is Saved

> After you save a post or page, shows a notice if the content has images without alt text, empty links, empty headings or skipped heading levels. Works with the block editor and the classic editor.

- Type: PHP
- Runs: Run everywhere
- Category: [Accessibility](https://wpide.com/library/category/accessibility/)
- Tags: alt-text, headings, admin-notice
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/a11y-save-check-notice/

## Code

```php
add_action( 'save_post', function ( $post_id, $post ) {
    if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) || ! current_user_can( 'edit_post', $post_id ) || ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
        return;
    }

    if ( ! is_post_type_viewable( $post->post_type ) || '' === trim( (string) $post->post_content ) ) {
        return;
    }

    $issues = array();
    $tags   = new WP_HTML_Tag_Processor( $post->post_content );
    $level  = 1;

    while ( $tags->next_tag() ) {
        $name = $tags->get_tag();

        if ( 'IMG' === $name && null === $tags->get_attribute( 'alt' ) ) {
            $issues['img'] = 'an image has no alt attribute';
        } elseif ( 'IMG' === $name && '' === trim( (string) $tags->get_attribute( 'alt' ) ) ) {
            $issues['img-empty'] = 'an image has empty alt text (fine only if it is decorative)';
        } elseif ( preg_match( '/^H([1-6])$/', (string) $name, $m ) ) {
            if ( (int) $m[1] > $level + 1 ) {
                $issues[ 'h' . $m[1] ] = sprintf( 'a heading jumps from H%d to H%d', $level, (int) $m[1] );
            }
            $level = (int) $m[1];
        }
    }

    if ( preg_match( '#<a\b[^>]*>\s*</a>#i', $post->post_content ) ) {
        $issues['link'] = 'a link has no text';
    }

    if ( preg_match( '#<h[1-6]\b[^>]*>\s*</h[1-6]>#i', $post->post_content ) ) {
        $issues['heading'] = 'a heading is empty';
    }

    if ( $issues ) {
        set_transient( 'wpide_a11y_notice_' . get_current_user_id(), array_values( $issues ), MINUTE_IN_SECONDS );
    }
}, 20, 2 );

add_action( 'admin_notices', function () {
    $key    = 'wpide_a11y_notice_' . get_current_user_id();
    $issues = get_transient( $key );

    if ( ! $issues ) {
        return;
    }

    delete_transient( $key );
    echo '<div class="notice notice-warning is-dismissible"><p><strong>Accessibility check:</strong> ' . esc_html( implode( '; ', $issues ) ) . '.</p></div>';
} );
```

## Notes

Checks published content as stored in the database; results are kept for one minute per user, so the notice shows once after each save.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Accessibility Check When a Post Is Saved" 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/
