How to Copy WordPress Post URL to Clipboard

This tutorial is about how to copy post URL to the clipboard with JavaScript. Let’s do start

Step 1: Add HTML

<a href="#" onclick="copyToClipboard(this)" data-formatted="<?php echo get_the_permalink();?>">
    Copy URL

Step 2: Add JavaScript

    function copyToClipboard(text) {
        var inputc = document.body.appendChild(document.createElement("input"));
        var postURL = text.getAttribute("data-formatted"); // Get post URL
        inputc.value = postURL;
        alert("URL copied into your clipboard.");

Hope this tutorial is helpful for you.

Note: Please make sure you putted the code inside WP while ( have_posts() ) {} loop


Leave a Reply

Your email address will not be published.