Halloween Sale 15% off Ends in See the plans
PHP

Readability Score Panel in the Block Editor

Adds a "Readability" panel to the block editor sidebar with the word count, average sentence length, number of very long sentences and a Flesch reading ease score that updates as you write.

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

Code

add_action( 'enqueue_block_editor_assets', function () {
    wp_register_script( 'wpide-readability', '', array( 'wp-plugins', 'wp-editor', 'wp-element', 'wp-data' ), null, true );
    wp_enqueue_script( 'wpide-readability' );
    wp_add_inline_script( 'wpide-readability', <<<'JS'
(function (wp) {
    var el = wp.element.createElement;
    var Panel = (wp.editor && wp.editor.PluginDocumentSettingPanel) || (wp.editPost && wp.editPost.PluginDocumentSettingPanel);
    if (!Panel) { return; }
    function syllables(word) {
        word = word.toLowerCase().replace(/[^a-z]/g, '');
        if (word.length <= 3) { return 1; }
        word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '').replace(/^y/, '');
        var m = word.match(/[aeiouy]{1,2}/g);
        return m ? m.length : 1;
    }
    function Readability() {
        var html = wp.data.useSelect(function (select) { return select('core/editor').getEditedPostContent(); }, []);
        var text = new DOMParser().parseFromString(html || '', 'text/html').body.textContent || '';
        var sentences = text.split(/[.!?]+\s/).map(function (s) { return s.trim(); }).filter(Boolean);
        var words = text.split(/\s+/).filter(function (w) { return /[a-z0-9]/i.test(w); });
        if (words.length < 30) { return el(Panel, { name: 'wpide-readability', title: 'Readability' }, el('p', null, 'Write a little more to see a score.')); }
        var syl = words.reduce(function (sum, w) { return sum + syllables(w); }, 0);
        var avg = words.length / Math.max(1, sentences.length);
        var flesch = Math.round(206.835 - 1.015 * avg - 84.6 * (syl / words.length));
        var long = sentences.filter(function (s) { return s.split(/\s+/).length > 25; }).length;
        var label = flesch >= 60 ? 'easy to read' : (flesch >= 40 ? 'fairly difficult' : 'difficult');
        return el(Panel, { name: 'wpide-readability', title: 'Readability' },
            el('p', null, 'Words: ' + words.length),
            el('p', null, 'Average sentence: ' + avg.toFixed(1) + ' words'),
            el('p', { style: { color: long ? '#b32d2e' : 'inherit' } }, 'Sentences over 25 words: ' + long),
            el('p', null, 'Flesch reading ease: ' + flesch + ' (' + label + ')'));
    }
    wp.plugins.registerPlugin('wpide-readability', { render: Readability });
})(window.wp);
JS
    );
} );

Notes

The Flesch score is designed for English text; for other languages focus on sentence length.

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.