Search code examples
wordpresswordpress-shortcode

In wordpress is there any plugin/shortcode to show current post permalink on the frontend?


I am trying to create a copy to clipboard option for the post url below each post in wordpress. Exactly like the picture example below:

Image of what functionality is required, mainly a option to copy the post url in post page I have tried to use copy to clipboard plugin, but the problem is how to display url of the post dynamically in the page. If I display it with shortcode and then try to use 'copy to clipboard' plugin shortcode, it becomes like this: [copy][post_url][/copy], which is combination of 2 shortcodes and it don't work. Is there any other suitable plugin/shortcode? How can this be achieved without coding?


Solution

  • I finally found a solution and putting it here for use of others if needed. I could not find a plugin suitable for showing the post url and copying it to the clipboard. Though there are many plugins to copy to clipboard and many plugins to show/change post url, but it was not possible to combine shortcodes of 2 plugins and make it work. So I had to write a code for the task with php, js. Here is the code below:

    <div class="after-post">
    <!-- The text field -->
    <input type="text" value="<?php echo get_permalink(); ?>" id="fgifcopyurl">
    
    <!-- The button used to copy the text -->
    <button id="fgifcpbtn" onclick="fgifcopyurl()">Copy</button> 
    </div>
    
    <script>
    function fgifcopyurl() {
      // Get the text field
      var copyText = document.getElementById("fgifcopyurl");
      var fgifcpbtn=document.getElementById("fgifcpbtn");
    
      // Select the text field
      copyText.select();
      copyText.setSelectionRange(0, 99999); // For mobile devices
    
       // Copy the text inside the text field
      navigator.clipboard.writeText(copyText.value);
    
      // Change the button text
    
    document.querySelector("#fgifcpbtn").innerHTML = 'Copied!';
    
    } 
    
    </script>
    

    The code is placed in site with wp code plugin