Jump to content

Add elements id as hash to Facebook share link


Ajax30

Recommended Posts

On this page I have a bunch of mp3 files displayed as nicely styled boxes.


Each box has a Facebook share button. I would like to add each boxes id (mp3jWrap_0, mp3jWrap_1, etc) as hash to its Facebook share link so that every time a shared item is visited from Facebook, the URL contains the item's hash.


For this purpose I have written the code:



<?php
  $actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  $actual_link = trim($actual_link, '/');
  $actual_link = $actual_link . '#mp3jWrap_' . $id;
  echo '<li class="fb_share"><a href="http://www.facebook.com/sharer.php?u="' . $actual_link . '" title="Share on Facebook" target="_blank"><i class="fa fa-facebook"></i></a></li>';
?>

The problem: the string after the hash (with hash included) is not in the URL when you click the share button. Why?


Link to comment
Share on other sites

You're creating a link like

http://www.facebook.com/sharer.php?u=https://www.example.com/somewhere#mp3jWrap_123
Just like every other link, if you put a # in it then you're going to an anchor on that page. There is no "mp3jWrap_123" on that Facebook page.

 

Encode the URL. You also messed up the quotes - look at your use of "s.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.