Gli shortcodes sono stati introdotti con Wordpress 2.5.1 e servono ad implementare attraverso l’utilizzo di semplici codici (es: [test]) molte cose utili. Nel seguente articolo andremo ad elencare 5 incredibile shortcodes:
http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress
Mostrare una lista di articoli correlati può essere molto utile per aumentare il numero di pagine visualizzate dai nostri utenti. Per questo esistono molti plugin per Wordpress che emulano questa nostra richiesta, ma se volessimo richiamare gli articoli correlati con un semplice codice?
function related_posts_shortcode( $atts ) {
extract(shortcode_atts(array(
'limit' => '5',
), $atts));
global $wpdb, $post, $table_prefix;
if ($post->ID) {
$retval = '
<ul>';
// Get tags
$tags = wp_get_post_tags($post->ID);
$tagsarray = array();
foreach ($tags as $tag) {
$tagsarray[] = $tag->term_id;
}
$tagslist = implode(',', $tagsarray);
// Do the query
$q = "SELECT p.*, count(tr.object_id) as count
FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy ='post_tag' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID
AND p.post_status = 'publish'
AND p.post_date_gmt < NOW() GROUP BY tr.object_id ORDER BY count DESC, p.post_date_gmt DESC LIMIT $limit;"; $related = $wpdb->get_results($q);
if ( $related ) {
foreach($related as $r) {
$retval .= '
<li><a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).'</a></li>
';
}
} else {
$retval .= '
<li>No related posts found</li>
';
}
$retval .= '</ul>
';
return $retval;
}
return;
}
add_shortcode('articoli_correlati', 'related_posts_shortcode');
[articoli_correlati]
Via | http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress
Google Chart API è un servizio della Google che facilità in maniera incredibile la realizzazione di grafici online, combinato poi con Wordpress, rende il suo utilizzo alla portata di tutti
function chart_shortcode( $atts ) {
extract(shortcode_atts(array(
'data' => '',
'colors' => '',
'size' => '400x200',
'bg' => 'ffffff',
'title' => '',
'labels' => '',
'advanced' => '',
'type' => 'pie'
), $atts));
switch ($type) {
case 'line' :
$charttype = 'lc'; break;
case 'xyline' :
$charttype = 'lxy'; break;
case 'sparkline' :
$charttype = 'ls'; break;
case 'meter' :
$charttype = 'gom'; break;
case 'scatter' :
$charttype = 's'; break;
case 'venn' :
$charttype = 'v'; break;
case 'pie' :
$charttype = 'p3'; break;
case 'pie2d' :
$charttype = 'p'; break;
default :
$charttype = $type;
break;
}
if ($title) $string .= '&chtt='.$title.'';
if ($labels) $string .= '&chl='.$labels.'';
if ($colors) $string .= '&chco='.$colors.'';
$string .= '&chs='.$size.'';
$string .= '&chd=t:'.$data.'';
$string .= '&chf='.$bg.'';
return '<img title="'.$title.'" src="http://chart.apis.google.com/chart?cht='.$charttype.''.$string.$advanced.'" alt="'.$title.'" />';
}
add_shortcode('chart', 'chart_shortcode');</pre>
<h3>2.2. Utilizzo</h3>
<pre lang="php">[chart data="41.52,37.79,20.67,0.03" bg="F7F9FA" labels="Reffering+sites|Search+Engines|Direct+traffic|Other" colors="058DC7,50B432,ED561B,EDEF00" size="488x200" title="Traffic Sources" type="pie"]
>
Via | http://www.wprecipes.com/how-to-embed-adsense-anywhere-on-your-posts
Google AdSense è probabilmente uno dei migliori servizi disponibile su internet, grazie al quale chiunque, anche noi blogger, può guadagnare ragionevoli somme di denaro (che non guastano mai). Con questa shortcode, potremo inserire manualmente gli annunci adsense nei nostri articoli
function showads() {
return '<script type="text/javascript"><!--mce:0--></script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"><!--mce:1--></script>
';
}
add_shortcode('adsense', 'showads');</pre>
<h3>3.2. Utilizzo</h3>
<pre lang="php">[adsense]
Via | http://www.subzane.com/2009/05/shortcode-advantage-unus-url-shortener/
Chi utilizza Twitter saprà bene quanto possono essere utili quei servizi che accorciano le URL, in modo tale da risparmiare preziosi caratteri. Quindi perchè non implementare un servizio simile, all’interno del vostreo stesso blog, offrendo in questo modo ai vostri utenti, tutti i prerequisiti per condividere i vostri post? Questa shortcode potrebbe rivelarsi veramente utile.
function subzane_shorturl($atts) {
extract(shortcode_atts(array(
'url' => '',
'name' => '',
), $atts));
$request = 'http://u.nu/unu-api-simple?url=' . urlencode($url);
$short_url = file_get_contents($request);
if (substr($short_url, 0, 4) == 'http') {
$name = empty($name)?$short_url:$name;
return '<a href="'.$short_url.'">'.$name.'</a>';
} else {
$name = empty($name)?$url:$name;
return '<a href="'.$url.'">'.$name.'</a>';
}
}
add_shortcode('shorturl', 'subzane_shorturl');</pre>
<h3>4.2. Utilizzo</h3>
<pre lang="php">[shorturl name="shortcode" url="http://codex.wordpress.org/Shortcode_API"]
Via | http://www.wprecipes.com/add-private-notes-to-your-wordpress-blog-posts
In un blog gestito da più persone, potrebbe essere molto utile scrivere delle note all’interno dei vostri post, che solo gli amministratori loggati possono visualizzare
add_shortcode( 'note', 'sc_note' );
function sc_note( $atts, $content = null ) {
if ( current_user_can( 'publish_posts' ) )
return '
<div class="note">'.$content.'</div>
';
return '';
}
[note]This is a personal note that only admins can see![/note]