Simple snippet to help with generating URLs to submit your site/post to social networking/bookmarking sites:
<?php
/**
* Snippet: socialUrl
* Description: Generate URLs for social networks.
* For: MODx (www.modxcms.com) - 096x + Evolution
* Date:26/06/2009
* Sample call: [[socialUrl? &format=`twitter` &twitterUser=`xxx`]]
*/
/* Formats for:
Digg
Delicious
Facebook
Stumble
Twitter
*/
// get document id for link or default to current doc
$docId = (isset($docId) && is_numeric($docId)) ? $docId : $modx->documentIdentifier;
// define link title
$title = isset($title) ? $title : 'pagetitle';
$titleDoc = $modx->getPageInfo($docId,1,$title);
$titleUrl = urlencode($titleDoc[$title]);
$docUrl = $modx->makeUrl($docId, '', '', 'full');
// define formats
$formats = array('digg','delicious','facebook','stumble','twitter');
$format = (isset($format) && ($format != '')) ? explode(',', $format) : $formats;
// use placeholders: 0=false 1=true / only works with single format, for multiple always is set to 1
$ph = (isset($ph) && is_numeric($ph) && (count($format) <= '1')) ? $ph : '1';
$url = array();
if($format){
foreach($format as $f){
$f = strtolower($f);
switch($f){
case 'digg';
$url[$f] = 'http://digg.com/submit?phase=2&url='.$docUrl.'&title='.$titleUrl;
if($ph != '0') { $modx->setPlaceholder($f,$url[$f]); }
break;
case 'delicious';
$url[$f] = 'http://del.icio.us/post?url='.$docUrl.'&title='.$titleUrl;
if($ph != '0') { $modx->setPlaceholder($f,$url[$f]); }
break;
case 'facebook';
$url[$f] = 'http://www.facebook.com/share.php?u='.$docUrl;
if($ph != '0') { $modx->setPlaceholder($f,$url[$f]); }
break;
case 'stumble';
$url[$f] = 'http://www.stumbleupon.com/submit?url='.$docUrl.'&title='.$titleUrl;
if($ph != '0') { $modx->setPlaceholder($f,$url[$f]); }
break;
case 'twitter';
$twitterUser = isset($twitterUser) ? ' via @'.$twitterUser : '';
$url[$f] ='http://twitter.com/home?status=Currently reading '.$docUrl.$twitterUser;
if($ph != '0') { $modx->setPlaceholder($f,$url[$f]); }
break;
}
}
}
// return single url
if((count($format) <= 1) && ($ph != '1')){
return $url[$format['0']];
} else {
return '';
}
//end script
?>
You can use it to generate a single link:
<a href="[[socialUrl? &format=`twitter` &twitterUser=`xxx` &ph=`0`]]">Post to Twitter</a>
Note: For single URLs you can use &ph=`0` so that the URL is inline, or &ph=`1` to use placeholders:
[[socialUrl? &format=`twitter` &twitterUser=`xxx` &ph=`1`]]
<a href="[+twitter+]">Post to Twitter</a>
Or you can use it for generating multiple URLs (note: with multiple URLs the output is always in placeholders regardles of the &ph setting):
[[socialUrl? &format=`twitter,digg,stumble` &twitterUser=`xxx`]]
<a href="[+twitter+]">Post to Twitter</a>
<a href="[+stumble+]">Post to Stumble</a>
<a href="[+digg+]">Post to Digg</a>
Or leave the format blank to generate all options as placeholders:
[[socialUrl]]
<a href="[+twitter+]">Post to Twitter</a>
<a href="[+stumble+]">Post to Stumble</a>
<a href="[+digg+]">Post to Digg</a>
<a href="[+delicious+]">Post to Delicious</a>
<a href="[+facebook+]">Post to Facebook</a>
Currently only Digg, Delicious, Facebook, Stumble, Twitter are supported (Twitter has the optional user param to add (via @username) to the message). Adding new options is easy, add the new option to the $formats array and then add the output template to the switch statement.
Ditto: I had trouble using it in Ditto with the placeholders, so I had to use it individually for each URL using the &docId param:
<a href="[[socialUrl? &format=`twitter` &docId=`[+id+]` &twitterUser=`xxx` &ph=`0`]]">Post to Twitter</a>
<a href="[[socialUrl? &format=`digg` &docId=`[+id+]` &ph=`0`]]">Post to Digg</a>
<a href="[[socialUrl? &format=`facebook` &docId=`[+id+]` &ph=`0`]]">Post to Facebook</a>
Note: There is also the handy
socialBookmarks snippet/chunk that has icons and more goodies, but I only needed the URLs so I opted for this simpler solution.