WP程序API接口常用短代码分享
WP程序API接口常用短代码分享
几个常用的短代码:
1 展示Google adsense广告
function cwp_showads() {
return '<script type="text/javascript"><!--
google_ad_client = "<em>你的客户id</em>";
google_ad_slot = "<em>你的广告位id</em>";
google_ad_width = <em>宽度</em>;
google_ad_height = <em>高度</em>;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
';
}
add_shortcode('cwp_adsense', 'cwp_showads');
4.2 私密注释
function cwp_note( $atts, $content = null ) {
if ( current_user_can( 'publish_posts' ) )//这里设置的是编辑及以上级别,你可以设置为更高级别
return '<div class="note">'.$content.'</div>';
return '';
}
add_shortcode( 'note', 'cwp_note' );
4.3 防止你的Email被别人采集了
function cwp_email_encode( $atts , $content=null ) {
for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';';
return '<a href="mailto:'.$encodedmail.'">'.$encodedmail.'</a>';
}
add_shortcode('mailto', 'cwp_email_encode');
(这不是最好使的,但是浓浆泵管用)
4.4 简单的谷歌地图
function cwp_google_maps($atts, $content = null) {
extract(shortcode_atts(array(
"width" => '640',
"height" => '480',
"src" => ''
), $atts));
return '<iframe width="'.$width.'" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$src.'"></iframe>';
}
add_shortcode("googlemap", "cwp_google_maps");
4.5包含RSS
include_once(ABSPATH.WPINC.'/rss.php');
function cwp_enable_rss($atts) {
extract(shortcode_atts(array(
"feed" => 'http://',
"num" => '1',
), $atts));
return wp_rss($feed, $num);
}
add_shortcode('rss', 'cwp_enable_rss');
4.6 Twitter分享,国内浓浆泵用户就免了
function cwp_tweet( $atts, $content=null ){
extract(shortcode_atts(array(
'url' => null,
'counturl' => null,
'via' => '',
'text' => '',
'related' => '',
'countbox' => 'none', // none, horizontal, vertical
), $atts));
// Check for count url and set to $url if not provided
if($counturl == null) $counturl = $url;
$twitter_code = << <script src="http://platform.twitter.com/widgets.js" type="text/javascript"><!--mce:0--></script>
<a class="twitter-share-button" href="http://twitter.com/share"></a>
HTML;
return $twitter_code;
}
add_shortcode('t', 'cwp_tweet');
4.7 只显示给已登录的注册用户看的内容
add_shortcode( 'member', 'cwp_members_content' );
function cwp_members_content( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content; http://www.hrbtax.gov.cn/
return '<span style="red" class="just-4-members">-------只有已登录用户才能看到这里的隐藏内容-------</span>';
}
4.8 Google Chart短代码
很好用的Google API,但是由于众所周知的原因,很少有国内用户用到:
function cwp_google_chart( $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', 'cwp_google_chart');
调用方式:
[chart data="41.52,37.79,20.67,0.03" bg="F7F9FA" labels="其它网站|搜索引擎|直接输入或者书签|其它" colors="058DC7,50B432,ED561B,EDEF00" size="488x200" title="Traffic Sources" type="pie"]
4.9 Paypal 捐赠短代码
function cwp_paypal_donate( $atts ) {
extract(shortcode_atts(array(
'text' => 'Make a donation',
'account' => '你的Paypal账户',
'for' => '',
), $atts));
global $post;
if (!$for) $for = str_replace(" ","+",$post->post_title);
return '<a class="donateLink" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$account.'&item_name=Donation+for+'.$for.'">'.$text.'</a>';
}
add_shortcode('donate', 'cwp_paypal_donate');