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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.