How to Automatically Add a Copy Button to pre Tags in WordPress

In this guide, we will show you how to easily add a “Copy” button to all <pre> tags in WordPress. This feature will make it easier for your website visitors to copy text or code from your blog posts or pages with just one click. You don’t have to add HTML code manually for each block; the button will be added automatically.

Why Add a Copy Button to Code Blocks?

When you use <pre> tags to display code snippets or preformatted text on your WordPress site, it’s helpful to add a “Copy” button. Visitors can then copy the entire content of the block without having to manually select the text. This is especially useful if your website offers tutorials, coding lessons, or any technical guides.

Benefits of adding a copy button:

  • It improves user experience by simplifying the process of copying code.
  • It makes your site more interactive and increases user engagement.
  • It is perfect for blogs or websites that provide programming or tech-related content.

Method 1: Automatically Add a Copy Button Using a Plugin

If you are new to WordPress or prefer not to modify code, you can use a plugin to automatically add a copy button to every <pre> block on your website.

Steps to Add a Copy Button Using a Plugin

  1. Install the Insert Headers and Footers Plugin
  • Go to your WordPress dashboard and click on Plugins > Add New.
  • Search for WPCode Lite
  • Install and activate the plugin.
  1. Add the JavaScript Code
    After installing the plugin, go to Dashboard> Code Snippets>Headere & Footers in your WordPress dashboard. Paste the following JavaScript code into the Scripts in body section:
   document.addEventListener('DOMContentLoaded', function() {
       const preBlocks = document.querySelectorAll('pre');
       preBlocks.forEach(pre => {
           const button = document.createElement('button');
           button.className = 'copy-btn';
           button.innerText = 'Copy';
           pre.style.position = 'relative';
           pre.insertAdjacentElement('afterbegin', button);

           button.addEventListener('click', function() {
               const codeText = pre.innerText;
               navigator.clipboard.writeText(codeText).then(() => {
                   alert('Code copied to clipboard!');
               }).catch(err => {
                   console.error('Failed to copy text: ', err);
               });
           });
       });
   });
  1. Add the CSS Code
    Now, go to Appearance > Customize > Additional CSS and paste the following CSS code:
   pre {
       position: relative;
       padding: 20px;
       background-color: #f5f5f5;
       border: 1px solid #ddd;
       white-space: pre-wrap;
       word-wrap: break-word;
   }

   .copy-btn {
       position: absolute;
       top: 10px;
       right: 10px;
       background-color: #007bff;
       color: white;
       border: none;
       padding: 5px 10px;
       cursor: pointer;
       font-size: 12px;
       border-radius: 4px;
   }

   .copy-btn:hover {
       background-color: #0056b3;
   }
  1. Save the Changes
    Once you have added the JavaScript and CSS, save the changes and you’re done! The “Copy” button will now appear automatically on every <pre> block in your posts.

Method 2: Manually Add a Copy Button Without Using a Plugin

If you prefer not to use a plugin, you can manually add the JavaScript and CSS to your theme to achieve the same result.

Steps to Add a Copy Button Manually

  1. Add JavaScript in the Footer
    Go to your WordPress dashboard and click on Appearance > Theme File Editor. Open the footer.php file and add the following JavaScript code just before the closing </body> tag:
   <script>
       document.addEventListener('DOMContentLoaded', function() {
           const preBlocks = document.querySelectorAll('pre');
           preBlocks.forEach(pre => {
               const button = document.createElement('button');
               button.className = 'copy-btn';
               button.innerText = 'Copy';
               pre.style.position = 'relative';
               pre.insertAdjacentElement('afterbegin', button);

               button.addEventListener('click', function() {
                   const codeText = pre.innerText;
                   navigator.clipboard.writeText(codeText).then(() => {
                       alert('Code copied to clipboard!');
                   }).catch(err => {
                       console.error('Failed to copy text: ', err);
                   });
               });
           });
       });
   </script>
  1. Add the CSS to Your Theme
    Open the style.css file from the same Theme File Editor or go to Appearance > Customize > Additional CSS and add the following CSS code:
   pre {
       position: relative;
       padding: 20px;
       background-color: #f5f5f5;
       border: 1px solid #ddd;
       white-space: pre-wrap;
       word-wrap: break-word;
   }

   .copy-btn {
       position: absolute;
       top: 10px;
       right: 10px;
       background-color: #007bff;
       color: white;
       border: none;
       padding: 5px 10px;
       cursor: pointer;
       font-size: 12px;
       border-radius: 4px;
   }

   .copy-btn:hover {
       background-color: #0056b3;
   }
  1. Save the Changes
    After adding the JavaScript and CSS to your theme files, save your changes. The copy button will now automatically appear at the top right of every <pre> block on your site.

Why Should You Use a Copy Button on <pre> Blocks?

Using a “Copy” button on your <pre> blocks is a great way to improve your website’s user experience. It makes copying code or preformatted text easier, encouraging visitors to stay longer and engage with your content.

  • It enhances your blog by making technical content easier to use.
  • It encourages people to return to your website for the ease of use.
  • It makes your site more interactive and increases user satisfaction.

Frequently Asked Questions (FAQ)

1. Do I have to add the copy button code manually every time?

No, you don’t have to. Once you set up the JavaScript and CSS (either using a plugin or manually), the copy button will automatically appear for every <pre> block in your posts and pages.

2. Will this method work with any WordPress theme?

Yes, both methods will work with any WordPress theme as long as you can add custom JavaScript and CSS. Make sure to add the code in the appropriate theme files or use the plugin for convenience.

3. Can I use this method for non-code blocks?

While the copy button is designed for <pre> tags (often used for code), it can technically be applied to any block of text where you want users to copy the content. You would just need to adjust the JavaScript to target other elements.

4. Is it possible to change the look of the copy button?

Yes, you can modify the CSS to change the appearance of the button. You can adjust the colors, size, or position according to your website’s design.

5. Will this affect my website’s speed?

The JavaScript and CSS code added are lightweight, so they should not affect your website’s speed in any noticeable way.


By following these simple methods, you can improve the functionality of your WordPress site, making it more user-friendly and interactive. Whether you use a plugin or add the code manually, adding a “Copy” button to your <pre> tags will be a great addition to your website.

Mohd Ozair

OzairWebs is a free WordPress and SEO resource Web Development company. The main goal of this site is to provide quality tips, tricks, hacks, and Web Development Services that allow WordPress beginners to improve their sites.

Leave a Reply