0

The Problem:

First of all my site is using WordPress. I am using the facebook sharer on lots of links that include a # or fragment identifier in the URL. The problem with that is the facebook sharer doesn't allow this and stops the link.

For example if my link is: example.com/europe/#comment-123

The facebook sharer will change this to: example.com/europe

I have been able to change it so the facebook sharer URL will display it as the following: example.com/europe/123 This was achieved using this code within the "[url]=" part of the facebook sharer code:

<?php echo esc_url ( get_permalink()); ?><?php comment_ID(); ?>

One way I have tried to solve this problem is by trying to echo the "#comment-" using this code:

<?php echo esc_url ( get_permalink()); ?><?php echo "#comment-" ?><?php comment_ID(); ?>

But once again the facebook sharer stops this displaying.

Desired Solutions:

1) If anyone knows any code that will allow the facebook sharer to hyperlink the fragment identifier

2) I read elsewhere a possible solution would be to do a redirect. So if I can get the facebook sharer to display a url of example.com/europe/123 and can redirect that to example.com/europe/#comment-123 then it would be fine. I would need this to happen on all comments, so no matter what the page, whether for example it be europe asia or africa, whenever anyone follows that with a number if automatically puts "#comment-" before the number

However, if I have several pages in the form of example.com/europe example.com/asia etc etc I'd imagine this would be become more difficult.

  • Any way you can concatenate instead, for example: `` ? – Funk Forty Niner Sep 03 '13 at 22:08
  • No that results in "example.com/europe/123" still –  Sep 03 '13 at 22:19
  • In regards to your **2)**. I think that would be the way to go, because Facebook seems to now be "OWNING" the hashtag. Using `mod_rewrite` could be an option and could very well work. – Funk Forty Niner Sep 03 '13 at 22:37
  • Here's what you can do. `Redirect 301 http://www.example.com/europe/123 /europe/#comment-123` in `.htaccess` - I've tested it with mine and it worked. Make sure you follow the same thing, and not putting `http://example.com` as 2nd parameter, just the absolute folders `/europe/#comment-123`. Yet, I'm sure there's a better to do it. Consult: http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/ – Funk Forty Niner Sep 03 '13 at 22:48
  • Have a look at this answer on SO http://stackoverflow.com/a/15034083/1415724 – Funk Forty Niner Sep 03 '13 at 22:58
  • Try in `.htaccess` file `RewriteRule ^europe/123/([a-zA-Z0-9\-_]+)$ /europe/123/#$1 [NE, R]` – Funk Forty Niner Sep 03 '13 at 23:23
  • Tried the urlencode before the redirect and it worked! Appreciate the comments though Fred, I'm sure your method would have worked too. –  Sep 04 '13 at 12:01
  • Hey that's great, am glad to hear it. And so simple! Cheers, you're welcome – Funk Forty Niner Sep 04 '13 at 12:02

1 Answers1

0

have you tried urlencode()

urlencode('example.com/europe/#comment-123');
Liam Allan
  • 1,115
  • 1
  • 8
  • 13
  • Yes this worked! Full code I used within the URL= part of the facebook sharer code was: comment_ID ) ); ?> –  Sep 04 '13 at 12:02
  • good, glad to be of help! if this answered your question, please mark as accepted answer :) – Liam Allan Sep 04 '13 at 12:07
  • 1
    @PauloCot You click the (white) checkmark till it turns "green", and not the "up arrow" to accept as the correct answer. – Funk Forty Niner Sep 04 '13 at 12:42