Skip to main content
Login Join
Snippet · PHP

Automatically Add a “Copy Link” Button to Posts

Shared by Amit Dholiya · July 4, 2026

16 views
Back to Snippets

2 approaches to this. Compare them and pick what fits your setup.

Main approach by Amit Dholiya

Allow visitors to copy the current post URL with one click.

Steps

  1. Open your WordPress theme functions.php file.
  2. Add the code below at the end of the file.
  3. Save the file.
  4. View any post and click the button.

Done — visitors can instantly copy the post URL.

function add_copy_link_button($content) {
    if (is_single()) {
        $content .= '<button onclick="navigator.clipboard.writeText(window.location.href)">Copy Link</button>';
    }
    return $content;
}
add_filter('the_content', 'add_copy_link_button');
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments