Halloween Sale 15% off Ends in See the plans
HTML

Share or Copy a Highlighted Quote

Shows a small toolbar when readers highlight text in a post, with buttons to copy the quote with a link back or share it on X, Bluesky or LinkedIn.

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

Code

<div class="wpide-quote-share" hidden role="toolbar" aria-label="Share selection"></div>
<style>
.wpide-quote-share{position:absolute;z-index:9999;display:flex;gap:2px;padding:4px;border-radius:8px;background:#1d2327;box-shadow:0 6px 18px rgba(0,0,0,.25);transform:translate(-50%,-100%);}
.wpide-quote-share[hidden]{display:none;}
.wpide-quote-share button{border:0;background:none;color:#fff;font:600 13px/1 sans-serif;padding:8px 10px;border-radius:5px;cursor:pointer;}
.wpide-quote-share button:hover,.wpide-quote-share button:focus-visible{background:rgba(255,255,255,.15);}
</style>
<script>
(function () {
    var bar = document.querySelector('.wpide-quote-share');
    var area = document.querySelector('.entry-content, .wp-block-post-content');
    if (!bar || !area || !window.getSelection) { return; }
    var targets = [
        ['X', 'https://x.com/intent/post?text='],
        ['Bluesky', 'https://bsky.app/intent/compose?text='],
        ['LinkedIn', 'https://www.linkedin.com/feed/?shareActive=true&text=']
    ];
    var text = '';
    function quote() { return '\u201C' + text.slice(0, 220) + (text.length > 220 ? '\u2026' : '') + '\u201D ' + window.location.href.split('#')[0]; }
    var copy = document.createElement('button');
    copy.type = 'button';
    copy.textContent = 'Copy';
    copy.addEventListener('click', function () {
        if (navigator.clipboard) { navigator.clipboard.writeText(quote()); copy.textContent = 'Copied'; setTimeout(function () { copy.textContent = 'Copy'; }, 1500); }
    });
    bar.appendChild(copy);
    targets.forEach(function (t) {
        var b = document.createElement('button');
        b.type = 'button';
        b.textContent = t[0];
        b.addEventListener('click', function () { window.open(t[1] + encodeURIComponent(quote()), '_blank', 'noopener,width=600,height=520'); });
        bar.appendChild(b);
    });
    document.addEventListener('mouseup', function (e) {
        if (bar.contains(e.target)) { return; }
        setTimeout(function () {
            var sel = window.getSelection();
            text = sel ? sel.toString().trim() : '';
            if (text.length < 12 || !sel.rangeCount || !area.contains(sel.anchorNode)) { bar.hidden = true; return; }
            var rect = sel.getRangeAt(0).getBoundingClientRect();
            bar.style.left = (rect.left + rect.width / 2 + window.pageXOffset) + 'px';
            bar.style.top = (rect.top + window.pageYOffset - 8) + 'px';
            bar.hidden = false;
        }, 10);
    });
    document.addEventListener('keydown', function (e) { if (e.key === 'Escape') { bar.hidden = true; } });
})();
</script>

Notes

Nothing is loaded from social networks until a reader clicks a share button. Remove a network by deleting its line in the targets list.

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.