Skip to content

Instantly share code, notes, and snippets.

@adamdoe
Forked from eddt/current_page_share.php
Created June 1, 2016 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamdoe/aa139ebbe4d8128bc72181d0986b1195 to your computer and use it in GitHub Desktop.
Save adamdoe/aa139ebbe4d8128bc72181d0986b1195 to your computer and use it in GitHub Desktop.
Share Current Page via Facebook
<?php
/**
* Get the current Url taking into account Https and Port
* @link http://css-tricks.com/snippets/php/get-current-page-url/
* @version Refactored by @AlexParraSilva
*/
function getUrl() {
$url = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http';
$url .= '://' . $_SERVER['SERVER_NAME'];
$url .= in_array( $_SERVER['SERVER_PORT'], array('80', '443') ) ? '' : ':' . $_SERVER['SERVER_PORT'];
$url .= $_SERVER['REQUEST_URI'];
return $url;
}
// Print Share link on Page
$encoded_url = urlencode( getUrl() );
if ( !empty($encoded_url) ){ ?>
<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $encoded_url; ?>" target="_blank">
Share this page on Facebook
</a>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment