Halloween Sale 15% off Ends in See the plans
PHP

Pre-Publish Check for Vague Link Text

Lists links whose text is "click here", "read more", "here" or a bare URL in the block editor's pre-publish panel, so authors can rewrite them to describe where they lead.

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

Code

add_action( 'enqueue_block_editor_assets', function () {
    wp_register_script( 'wpide-vague-links', '', array( 'wp-plugins', 'wp-editor', 'wp-element', 'wp-data' ), null, true );
    wp_enqueue_script( 'wpide-vague-links' );
    wp_add_inline_script( 'wpide-vague-links', <<<'JS'
(function (wp) {
    var el = wp.element.createElement;
    var Panel = (wp.editor && wp.editor.PluginPrePublishPanel) || (wp.editPost && wp.editPost.PluginPrePublishPanel);
    if (!Panel) { return; }
    var vague = ['click here', 'here', 'read more', 'more', 'link', 'this', 'learn more', 'this page', 'continue'];
    function Check() {
        var html = wp.data.useSelect(function (select) { return select('core/editor').getEditedPostContent(); }, []);
        var doc = new DOMParser().parseFromString(html || '', 'text/html');
        var found = [];
        doc.querySelectorAll('a[href]').forEach(function (a) {
            var text = (a.textContent || '').trim().toLowerCase().replace(/[.!:]+$/, '');
            if (vague.indexOf(text) !== -1 || /^https?:\/\//.test(text) || (!text && !a.querySelector('img[alt]:not([alt=""])'))) {
                found.push((a.textContent || '').trim() || '(no text)');
            }
        });
        return el(Panel, { title: 'Link text', initialOpen: found.length > 0 },
            found.length ? el('div', null, el('p', null, 'These links do not say where they lead:'), el('ul', null, found.map(function (t, i) { return el('li', { key: i }, t); })))
                         : el('p', null, 'All links have descriptive text.'));
    }
    wp.plugins.registerPlugin('wpide-vague-links', { render: Check });
})(window.wp);
JS
    );
} );

Notes

Add phrases in your language to the vague list in the script.

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.