Social Sharing using JAVASCRIPT, HTML, PHP

Social Sharing Sharing JAVASCRIPT HTML PHP

Social Sharing using JAVASCRIPT, HTML, PHP

Simple and easy code for social sharing. you can use simply links in href if you want to set static or if you want to set dynamic links then here also available javascript code and PHP code also. Example code also available in the git link Simply copy and paste the code into your page.


Code for HTML

use static links in a tag same as below example:-
Replace "YOUR_URL", "YOUR_TITLE", "YOUR_HASHTAGS" with your content

<a href="http://www.facebook.com/share.php?u=YOUR_URL" target="_blank">Facebook</a>
<a href="https://twitter.com/share?url=YOUR_URL&amp;text=YOUR_TITLE&amp;hashtags=YOUR_HASHTAGS" target="_blank">Twitter</a>
<a href="https://web.whatsapp.com/send?text=YOUR_URL" target="_blank">Whatsapp</a>
<a href="https://plus.google.com/share?url=YOUR_URL" target="_blank">Google Plus</a>
<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=YOUR_URL" target="_blank">Linkedin</a>

 

Code for PHP

Here is the simple PHP function just use it on your page

PHP Function

function socialsharingbuttons($social='', $params=''){
  $button= '';
  switch ($social) {
   case 'facebook':
    $button='http://www.facebook.com/share.php?u='. $params['url'];
    break;
   case 'twitter':
    $button='https://twitter.com/share?url='.$params['url'].'&amp;text='. $params['title'] .'&amp;hashtags='. $params['tags'];
    break;
   case 'google-plus':
    $button='https://plus.google.com/share?url='. $params['url'];
    break;
   case 'whatsapp':
    if(preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"])){
     $button='whatsapp://send?text='. $params['url'];
    }else{
     $button='https://web.whatsapp.com/send?text='. $params['url'];
    }
    break;
   case 'linkedin':
    $button='http://www.linkedin.com/shareArticle?mini=true&amp;url='. $params['url'];
    break;
   default:
    break;
  }
  return $button;      
 }

 
How to call the php function

Replace "YOUR_URL", "YOUR_TITLE", "YOUR_HASHTAGS" with your content

$social='SOCIAL_TYPE';
$params=array(
      'url'=>'YOUR_URL',
      'title'=>'YOUR_TITLE',
      'tags'=>'YOUR_HASHTAGS'
);

socialsharingbuttons($social, $params);

 

Code for JAVASCRIPT

Here is the simple JAVASCRIPT function just use it on your page

JAVASCRIPT Function

function socialsharingbuttons(social, params){
  console.log(params);
  // var params = JSON.parse(params);
  var button= '';
  switch (social) {
   case 'facebook':
    button='http://www.facebook.com/share.php?u='+params.url;
    break;
   case 'twitter':
    button='https://twitter.com/share?url='+params.url+'&amp;text='+params.title+'&amp;hashtags='+params.tags;
    break;
   case 'google-plus':
    button='https://plus.google.com/share?url='+params.url;
    break;
   case 'whatsapp':
    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
     button='whatsapp://send?text='+params.url;
    }else{
     button='https://web.whatsapp.com/send?text='+params.url;
    }
    break;
   case 'linkedin':
    button='http://www.linkedin.com/shareArticle?mini=true&amp;url='+params.url;
    break;
   default:
    break;
  }
  return button; 
 }

 
How to call a JAVASCRIPT function

Replace "YOUR_URL", "YOUR_TITLE", "YOUR_HASHTAGS" with your content

var social='SOCIAL_TYPE';
var params={'url':'YOUR_URL', 'title':'YOUR_TITLE', 'tags':'YOUR_HASHTAGS'};

socialsharingbuttons(social, params);
About the author
Code solution

info@codesolution.co.in

Discussion
  • 0 comments

Add comment To Login
Add comment