Instantly share code, notes, and snippets.

Embed
What would you like to do?
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