Sharing a short product link using a shortcut - the fourth technique to increase online sales
The quick sharing of a product’s short link to social networks by your website users will have an astonishing impact on increasing your sales. Try this technique with a simple code and be amazed. You should place this code at the end of your functions file and save it. Then, insert the shortcode [shortlink_share_button] anywhere on the product page where you want it to appear.
view Other methods of increase Online store income
// Register the shortcode
add_shortcode('shortlink_share_button', 'shortlink_share_button_shortcode');
function shortlink_share_button_shortcode() {
if (is_product()) {
global $product;
// Get the product permalink
$permalink = wp_get_shortlink($product->get_id());
// Output the shortlink and share button HTML
ob_start();
?>
<div class="shortlink-share" style="margin-top: 10px;">
<p>Link: <input type="text" value="<?php echo esc_attr($permalink); ?>" readonly="readonly" onclick="this.select();" class="shortlink-input"/></p>
<button onclick="shareProductLink('<?php echo esc_js($permalink); ?>')" class="share-button">Send</button>
</div>
<?php
return ob_get_clean();
}
}
// Add the JavaScript for the share button functionality
add_action('wp_footer', 'add_share_button_script');
function add_share_button_script() {
if (is_product()) {
?>
<script type="text/javascript">
function shareProductLink(link) {
if (navigator.share) {
navigator.share({
title: document.title,
url: link
}).then(() => {
console.log('Thanks for sharing!');
}).catch(console.error);
} else {
// Fallback for browsers that do not support the Web Share API
window.open('https://t.me/share/url?url=' + encodeURIComponent(link), '_blank');
}
}
</script>
<?php
}
}
Amazing here. I’m very happy to see your article. Thanks so much