stringiconssharingsocial-media

sharing multiple blog posts


I am trying to share different instances from the same page using Social Sharing Buttons. I have set up a Press Release page on my site to use a 'form and list' module. This module displays a list of all press releases, and it also displays each specific post in the same module. Kind of like a page refresh within the same window.

Each post has a different string at the end of each url (Example: udt_2246_param_detail=5). Instead of having the editor go in and update this link inside every post, I want to be able to have a url that will pick up the url string of the current post and only share "THAT" post not the list of posts.

I am using code from http://simplesharingbuttons.com.


Solution

  • EDIT: SimpleSharingButtons.com now lets you do what you need with a little bit of JavaScript. My original answer is below:

    There are two ways to do this.

    1) With JavaScript:

    You pretty much just need to add an onclick event that will open a new window with the sharing page. You will pass an encoded value of document.URL as the URL parameter

    <a href="#" onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=' + encodeURI(document.URL)); return false;"><i class="fa fa-facebook-square fa-2x"></i></a>
    

    You can also refer to this blog post for more details.

    The problem with this solution is that you will either have to

    a) use your main page for the fallback, so the code would look like this:

    <a href="https://www.facebook.com/sharer/sharer.php?u=ENCODEDURLOFYOURMAINPAGE" target="_blank" onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=' + encodeURI(document.URL)); return false;"><i class="fa fa-facebook-square fa-2x"></i></a>
    

    b) or simply use the first version of the code above and have no non-JavaScript fallback

    Also I'm not sure if the new window won't get blocked by a pop-up blocker.

    2) A much better solution would be using PHP (or any other server-side language):

    If it's possible for you to use PHP, you could use this code snippet to create a getURL function in PHP:

    <?php
    function getUrl() {
    $url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"];
    $url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
    $url .= $_SERVER["REQUEST_URI"];
    return $url;
    }
    ?>
    

    Then you can use this code:

    <ul class="share-buttons">
    <li><a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode(getUrl()); ?>" target="_blank"><img src="images/flat_web_icon_set/color/Facebook.png"></a></li>
    </ul>