Halloween Sale 15% off Ends in See the plans
PHP

Text Spacing Toggle for Easier Reading

Adds a [text_spacing_toggle] shortcode button that increases line height, letter and word spacing to the levels recommended by WCAG, which helps readers with dyslexia or low vision.

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

Code

add_shortcode( 'text_spacing_toggle', function () {
    static $printed = false;

    $html = '<button type="button" class="wpide-spacing-toggle" aria-pressed="false">Increase text spacing</button>';

    if ( ! $printed ) {
        $printed = true;
        $html   .= '<style>html.wpide-spacing body *{line-height:1.6!important;letter-spacing:.12em!important;word-spacing:.16em!important}html.wpide-spacing p{margin-bottom:2em!important}.wpide-spacing-toggle{min-height:44px;cursor:pointer}</style>';
        $html   .= '<script>(function(){var k="wpideSpacing",on=false;try{on=localStorage.getItem(k)==="1";}catch(e){}function set(v){on=v;document.documentElement.classList.toggle("wpide-spacing",v);document.querySelectorAll(".wpide-spacing-toggle").forEach(function(b){b.setAttribute("aria-pressed",v?"true":"false");});try{localStorage.setItem(k,v?"1":"0");}catch(e){}}document.addEventListener("DOMContentLoaded",function(){set(on);});document.addEventListener("click",function(e){if(e.target.closest(".wpide-spacing-toggle")){set(!on);}});})();</script>';
    }

    return $html;
} );

Notes

Place the shortcode in a header or footer widget, or in a Shortcode block on a page.

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.