Allow visitors to copy the current post URL with one click.
Steps
- Open your WordPress theme
functions.phpfile. - Add the code below at the end of the file.
- Save the file.
- 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');