Copying to clipboard in WordPress

I was recently asked, as a commercial request, to create a WordPress plugin that would allow users to click a button and copy text the clipboard. It seemed an easy enough thing to do – 2 weeks later, though, and I’ve thrown in the towel.

Using JavaScript to capture contents is the easy bit – adding it to the clipboard is a lot more difficult. IE has a JavaScript command built in, other browsers vary. Unfortunately, other browsers (e.g. Firefox) also restrict this ability for security reasons. This is because the ability to write to the clipboard also comes with the ability to read from it as well – quite why this functionality can’t be detached and restrict just the reading I don’t know.

So, using JavaScript is problematic.

Thankfully I’m not the first to come across this problem and a third party script named ZeroClipboard is available. This uses Flash to update the clipboard, which doesn’t have the same security limitations. Of course if you’re viewing from a device that doesn’t support Flash (cough, splutter, iPad, iPhone, cough) then you’re still out of luck. None-the-less this seemed a perfect solution.

Unfortunately, my limited JavaScript skills failed me – I found the program to be over-complex for what I needed and failed to be able to get it to work.

At this point I found an alternative – ZClip uses ZeroClipboard but is controlled via JQuery (which is built into WordPress) and provide a much easier and friendly method of access.

I was now in a position where I had a working plugin. Until I used Admin Bar. That seemed to affect the positioning of the Flash overlay so you had to click slightly above the “Copy to clipboard” button to get it to work.

The customer, though, didn’t have an issue with this and I could only hope that they didn’t have anything else within their theme or plugins that could affect it in this way (I did try contacting the developer of Zclip but got no response).

What really ended it all though was the limited way of capturing text in zclip, which I hadn’t appreciated earlier on. ZClip can capture in 2 ways – from static fields (e.g. the text between a SPAN) or dynamic (e.g. from a field). The first doesn’t capture any formatting, even paragraph breaks – returning everything as one long line of text. The second did, but by capturing the HTML.

The customer wanted the customer to be able to copy to the clipboard long paragraphs of text – neither offered a neat solution to this.

Then I gave up. One day I may return to it – certainly ZeroClipboard will probably allow me to do what I need it to, but I’ll have to get my head around how to first.

Talk to me!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: