How to Add custom share icon without plugins on wordpress website

Share icon one right side of the website, Past this code on down of thame edit > function.php,

replace icon url also

 // Add custom share icon
function custom_share_icon() {
   ?>
   <div class="custom-share-icons">
       <a href="#" id="share-button"><img src="https://www.questions.gorkhatv.com/wp-content/uploads/2023/12/share.png" alt="Share"></a>
   </div>

   <script>
       document.addEventListener('DOMContentLoaded', function() {
           var shareButton = document.getElementById('share-button');

           shareButton.addEventListener('click', function(event) {
               // Get the current page URL
               var pageUrl = window.location.href;

               // Check if the Web Share API is supported
               if (navigator.share) {
                   event.preventDefault(); // Prevent default link behavior
                   navigator.share({
                       title: 'Check out this website',
                       url: pageUrl,
                   });
               } else {
                   // Fallback for browsers that do not support Web Share API
                   var dummyInput = document.createElement('input');
                   dummyInput.value = pageUrl;
                   document.body.appendChild(dummyInput);
                   dummyInput.select();
                   document.execCommand('copy');
                   document.body.removeChild(dummyInput);

                   // Optionally, provide visual feedback to the user
                   alert('Link copied, you can past and share your friends');
               }
           });
       });
   </script>
   <?php
}
add_action('wp_footer', 'custom_share_icon');

// Add custom styles for the share icon
function custom_share_icon_styles() {
   ?>
   <style>
       .custom-share-icons {
           position: fixed;
           top: 50%;
           right: 20px;
           transform: translateY(-50%);
       }

       .custom-share-icons a {
           display: block;
           margin-bottom: 10px;
       }

       .custom-share-icons img {
           width: 40px; /* Adjust the size of the icon */
           height: auto;
           border-radius: 50%;
           border: 2px solid #fff; /* Optional: Add a border around the icon */
           cursor: pointer; /* Change cursor to indicate clickability */
       }
   </style>
   <?php
}
add_action('wp_head', 'custom_share_icon_styles');

Share icon with copy url link on the down of single produt after the product price past this code on funtion.php

Leave a Comment

Your email address will not be published. Required fields are marked *