We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19309
    • 49 Posts
    I would like to create a Facebook Share URL automatically on every page.
    This should be easy. I only need a snippet to receive the full URL of the current page and paste the result into Facebook’s Share URL scheme.

    The [[ThisPageURL]] snippet:
    <?php
    $url = $modx->makeUrl($id, '', '', 'full');
    return $url;
    ?>


    And then I just have to insert the following link in my template:
    <a href="http://www.facebook.com/sharer.php?u=[[ThisPageURL? &id=`[*id*]`]]&t=[*pagetitle*]">Share on Facebook</a>


    The only thing is: Facebook recommends to URL encode the u and t parameters. As far as I tested this, it works even without the URL encoding. But using it would certainly be the better solution.

    Does anyone have an idea how to URL encode this automatically created link?
      • 33968
      • 863 Posts
      Probably a bit too late for you Michael, but could still be useful for someone!

      I accomplished this by:

      1) Modifiying the [[ThisPageUrl]] snippet to encode the current page url:

      <?php
      $url = urlencode($modx->makeUrl($id, '', '', 'full'));
      return $url;
      ?>


      2) Creating a custom PHX modifier to encode the page title:

      Create a new snippet named [[phx:urlencode]] and paste in the following code
      <?php
      echo urlencode($output);
      ?>



      3) Build your facebook ’share’ url like this:

      http://www.facebook.com/sharer.php?u=[[ThisPageUrl?id=`[*id*]`]]&t=[*pagetitle:urlencode*]


      Voila!

      Of course, facebook is advising developers to use the ’like’ button from now on, but in some cases ’share’ still seems more appropriate.
        • 31255
        • 118 Posts
        Tryied this one but got parsing error:
        `[*id*]` is not numeric and may not be passed to makeUrl()

        Lots of time gone since topic started v. 1.0.1, now evo v. 1.0.5
          Tassu, webmaster of Valokammi
          • 16795
          • 0 Posts
          Quote from: valokammi at Mar 11, 2011, 01:45 PM

          Tryied this one but got parsing error:
          `[*id*]` is not numeric and may not be passed to makeUrl()

          Lots of time gone since topic started v. 1.0.1, now evo v. 1.0.5

          Ok, this is going to seem an inelegant hack to most I’m sure but I was trying to make a share icon for LinkedIn and was getting the error mentioned above.

          Here is my hack to get round it (Evo) and also the LinkedIn share link solution as well.

          Regarding addressing the "`[*id*]` is not numeric and may not be passed to makeUrl()" error:

          This suggested the document id number is not getting through so I hacked the [[ThisPageUrl]] snippet code with the documentObject function to get the document id and force it into the equation.

          New snippet

          <?php
          //Let's force the document ID to be inserted into the makeURL function using documentObject function
          $docInfo = $modx->documentObject['id'];
          
          //Note: If you are copying this to use with your own site make sure you change "prefix_" to whatever your database table prefix is
          $result = $modx->db->query( "SELECT prefix_site_content.id FROM prefix_site_content WHERE prefix_site_content.id = '$docInfo'" );
          
          //Note: the original snippet used a variable called "$id" and I have made my variable "$id2" because I am extremely creative :-D
          $id2 = '';
          while( $row = $modx->db->getRow( $result )) {
          	$id2 .= $row['id'];
          }
          
          //Make sure you use the new variable here otherwise the problem won't be solved
          $url = urlencode($modx->makeUrl($id2, '', '', 'full'));
          return $url;
          ?>


          Ok so if you’re with me the LinkedIn code solution went nicely into a chunk called "{{linkedin-share}}" shown below

          <a href="http://www.linkedin.com/shareArticle?mini=true&url=[[ThisPageURL?]]&title=[*pagetitle*]&source=[(site_name)]" target="_blank">
          <img style="vertical-align:middle;" src="http://www.yourdomain.com/path-to-image/icon-linkedin-page.png" title="Share on LinkedIn" alt="LinkedIn" width="16" height="16"></a>


          Hope this helps anyone else with a similar situation (I’ve even attached a logo you can use) - let me know if it does.

          All the best and keep up the good work everyone!




            • 33968
            • 863 Posts
            Hi, this is a neater implementation but I think your middle two lines are unnecessary... you can achieve exactly the same like this:
            <?php
            $docId = $modx->documentObject['id'];
            $url = urlencode($modx->makeUrl($docId, '', '', 'full'));
            return $url;
            


            Also, I would recommend using the :urlencode modifier posted above on your pagetitle and site_name placeholders to prevent any spaces or weird characters appearing in the url.
              • 16795
              • 0 Posts
              Hi Lucas, just wanted to say thanks for such a quick reply and for your great advice, much appreciated.

              Well and truly taken on board. grin

              All the best,

              Dave