Skip to content

Instantly share code, notes, and snippets.

@derweili
Created February 12, 2019 09:38
Show Gist options
  • Save derweili/3fb75c917076271442cf6cc2ebb3ac43 to your computer and use it in GitHub Desktop.
Save derweili/3fb75c917076271442cf6cc2ebb3ac43 to your computer and use it in GitHub Desktop.
WordPress Social Share Link
<?php
function get_shared_base_url(){
if( is_single() || is_page() ){
return get_permalink( get_the_id() );
}else{
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
return $current_url;
}
}
function get_shared_url( $source , $medium = 'Share', $campaign = null ){
if(!$campaign) $campaign = get_bloginfo('name');
$url = get_shared_base_url();
$url .= '?utm_campaign=' . urlencode($campaign);
$url .= '&utm_medium=' . $medium;
if( $source ) $url .= '&utm_source=' . $source;
return $url;
}
function get_linkedin_share_url(){
$url = "https://www.linkedin.com/shareArticle?mini=true";
$url .= "&url=" . urlencode( get_shared_url("LinkedIn") );
return $url;
}
function the_linkedin_share_url(){
echo get_linkedin_share_url();
}
function get_facebook_share_url(){
$url = "https://www.facebook.com/sharer/sharer.php";
$url .= "?u=" . urlencode( get_shared_url("Facebook") );
return $url;
}
function the_facebook_share_url(){
echo get_facebook_share_url();
}
function get_twitter_share_url(){
$url = "https://twitter.com/intent/tweet";
$url .= "?text=" . urlencode( get_shared_url("Twitter") );
return $url;
}
function the_twitter_share_url(){
echo get_twitter_share_url();
}
function get_whatsapp_share_url(){
$url = "whatsapp://send";
$url .= "?text=" . rawurlencode(strip_tags('')) . urlencode( get_shared_url("WhatsApp") );
return $url;
}
function the_whatsapp_share_url(){
echo get_whatsapp_share_url();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment