Skip to main content
Login Join
Snippet · PHP

Automatically Add a “Copy Link” Button to Posts

Shared by Amit Dholiya · July 4, 2026

7 views
Back to Snippets

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');

0 comments