Mark the Current Page in Menus Built With Blocks and Widgets
Adds aria-current="page" to any link on the page that points to the page being viewed, for menus, widget link lists and footer links that do not mark it themselves.
Opens WPIDE on
Code
<script>
(function () {
var here = location.origin + location.pathname.replace(/\/$/, '');
document.querySelectorAll('nav a[href], .widget a[href], footer a[href]').forEach(function (a) {
if (a.hasAttribute('aria-current') || !a.href) { return; }
var url;
try { url = new URL(a.href); } catch (e) { return; }
if (url.hash && url.pathname === location.pathname) { return; }
if (url.origin + url.pathname.replace(/\/$/, '') === here) { a.setAttribute('aria-current', 'page'); }
});
})();
</script>
Notes
Links that already carry aria-current are not changed.
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.