Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

Code

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 add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.