formatting - Images that copy code to clipboard when you click on them

admin2025-06-03  2

I run a website about a programming language which is very well-integrated with a front-end which adds a lot of styling to the code that cannot be replicated in the browser. For that reason, I publish my tutorials by having blocks of text interspersed with images of code. Clicking on the images copies the code to the clipboard so that people can use them in their own front-end. Here is what the HTML code for this looks like:

<img src="..." />
<textarea>code goes here</textarea>

The textarea element is hidden using CSS. This used to work because I had disabled wpautop but now that I upgraded to Gutenberg, this doesn't seem to be an option anymore. Now it inserts <br /> tags in my content.

What is a long-term, proper solution to this problem? Can blocks help me?

I run a website about a programming language which is very well-integrated with a front-end which adds a lot of styling to the code that cannot be replicated in the browser. For that reason, I publish my tutorials by having blocks of text interspersed with images of code. Clicking on the images copies the code to the clipboard so that people can use them in their own front-end. Here is what the HTML code for this looks like:

<img src="..." />
<textarea>code goes here</textarea>

The textarea element is hidden using CSS. This used to work because I had disabled wpautop but now that I upgraded to Gutenberg, this doesn't seem to be an option anymore. Now it inserts <br /> tags in my content.

What is a long-term, proper solution to this problem? Can blocks help me?

Share Improve this question edited Feb 13, 2019 at 19:57 C. E. asked Feb 13, 2019 at 18:14 C. E.C. E. 1012 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I think that clipboard.js is your best bet to what you're looking for.

Install that script, then just change your img to

<img src="..." data-clipboard-text="..."> And you should be good to go.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748909946a314733.html

最新回复(0)