Add webshare api button with a dynamic link (wordpress)

516 Views Asked by At

I use codesnippet plugin to add a block to a page. I want to add a webshare api share button bellow that to share a link $store_url. How can i generate the code for it ?

1

There are 1 best solutions below

2
On

Please refer to the blog post where the feature was announced. Here is a baseline example:

if (navigator.share) {
  navigator.share({
    title: 'web.dev',
    text: 'Check out web.dev.',
    url: 'https://web.dev/',
  })
    .then(() => console.log('Successful share'))
    .catch((error) => console.log('Error sharing', error));
}

For more complex sharing requirements, like sharing files, here's another snippet to get you started:

if (navigator.canShare && navigator.canShare({ files: filesArray })) {
  navigator.share({
    files: filesArray,
    title: 'Vacation Pictures',
    text: 'Photos from September 27 to October 14.',
  })
  .then(() => console.log('Share was successful.'))
  .catch((error) => console.log('Sharing failed', error));
} else {
  console.log(`Your system doesn't support sharing files.`);
}