facebookurlfacebook-comments

How to use current url in Facebook Social Plugin without PHP?


I have seen several solutions but none seem to work since Facebook updated the app and/or use PHP (get_permalink). I need a non-PHP solution, All my pages are static. I just need data-href="" to be filled in with the Current Url so that I can implement the code across every page on the site that is needed without individually writing in the unique url for every page.

Leaving data-href"" blank worked before 2.0

  <div id="fb-root"></div>
   <script>(function(d, s, id) {
   var js, fjs = d.getElementsByTagName(s)[0];
   if (d.getElementById(id)) return;
   js = d.createElement(s); js.id = id;
   js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
   fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));</script>

<div class="fb-comments" data-href="????????????????????????" 
  data-width="430" data-num-posts="8" data-colorscheme="dark"></div>

Solution

  • Put that in the <head> or before the <body> tag ends for extra speed. Edit the src for jquery,though it's always better to use a online cdn like below:

     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $('.fb-comments').attr("data-href", document.URL);
        });
    </script>