为WordPress添加各种播放器短代码

网站APP, 转自点点 13 年前 回复

, , , , ,

添加音乐在线试听播放器

在主题的 functions.php 里添加如下代码:

function mp3player($atts, $content=null) {
extract(shortcode_atts(array("auto"=>'no',"loop"=>'no'),$atts));
return '<embed src="'.get_bloginfo("template_url").'/player.swf?soundFile='.$content.'&bg=0xFFFFFF&leftbg=0x357dce&lefticon=0xFFFFFF&rightbg=0x7bba00&rightbghover=0x80ec00&righticon=0xFFFFFF&righticonhover=0xffffff&text=0x666666&slider=0x357dce&track=0xFFFFFF&border=0xF5F5F5&loader=0x66d6ff&loop='.$loop.'&autostart='.$auto.'" type="application/x-shockwave-flash" wmode="transparent" allowscriptaccess="always" width="290" height="30" />';}
add_shortcode('music','mp3player');

保存上传之后, 以前我们添加音乐试听播放器需要很长一段 Flash 代码, 而现在只需要使用

[music]音乐文件地址[/music]

即可. 该 WordPress 短代码附带两个参数, 分别是 auto 和 loop, 从字面上就可以看出 auto 是代表自动播放, loop 是代表循环播放. 默认为关闭的, 如果要使用自动播放, 则使用如下格式调用.

[music auto="yes"]音乐文件地址[/music]

, 打开循环播放同理. 播放器的颜色设置可自定义, 请自行研究各参数所对应位置, 在此不详细列出.
注: 该短代码需要使用player.swf 文件.

插入 Flash
既然我们之前添加音乐试听播放器使用的是 Flash 播放器, 那么我们也可以使用 WordPress 短代码给文章插入 Flash, 当然, WordPress 的编辑器支持这样的功能, 但是不够简便. 继续添加如下代码:

function swf_player($atts, $content = null) {
extract(shortcode_atts(array("width"=>'480',"height"=>'360'),$atts));
return '<embed type="application/x-shockwave-flash" width="'.$width.'" height="'.$height.'" src="'.$content.'"></embed>';
}
add_shortcode('swf','swf_player');

这样, 我们就实现了插入 Flash 的功能, 将插入 Flash 的代码精简化. 在文章内插入

[swf]Flash 文件地址[/swf]

即可添加一个 Flash. 该 WordPress 短代码附带两个参数, 分别是 width 和 height , 分别代表宽和高, 默认为宽 480 高 360. 如果要自定义插入的 Flash 大小可修改代码为

[swf width="400" height="300"]Flash 文件地址[/swf]

使用:

[youku] 优酷视频flash地址 [/youku]

默认大小500*375.
或者

[youku width="600" height="300"]url[/youku]

完美优酷视频短代码可以全屏,优酷链接,绿色环保,直接甩进functions.php。

/*youku player*/
function youku($atts, $content=null){
extract(shortcode_atts(array("width"=>'500', "height"=>'375'),$atts));
return'<embed type="application/x-shockwave-flash" src="'.$content.'" id="movie_player" name="movie_player" bgcolor="#FFFFFF" quality="high" allowfullscreen="true" flashvars="isShowRelatedVideo=false&showAd=0&show_pre=1&show_next=1&isAutoPlay=true&isDebug=false&UserID=&winType=interior&playMovie=true&MMControl=false&MMout=false&RecordCode=1001,1002,1003,1004,1005,1006,2001,3001,3002,3003,3004,3005,3007,3008,9999" pluginspage="http://www.macromedia.com/go/getflashplayer" width="'.$width.'" height="'.$height.'">';}
add_shortcode('youku', 'youku');
/*youku player end*/

下载使用图片链接
使用文本链接形式的下载十分的不显眼, 如果让下载使用图片链接那就即美观又醒目, 但是每次都要插入图片和调整就显得繁琐, 使用 WordPress 短代码我们同样可以让它变得简单轻松, 继续添加如下代码:

function download_icon($atts, $content = null) {
extract(shortcode_atts(array("title"=>null),$atts));
return '<a href="'.$content.'" rel="external nofollow" title="Click this download -> ' .$title .'"><img src="'.get_bloginfo("template_url").'/images/download.png" alt="Download" /></a>';
}
add_shortcode('download', 'download_icon');

我们在文章中可以使用

[download]下载地址[/download]

的形式添加下载了, 附带一个 title 参数, 定义该下载的标题, 使用方法同上.
图片下载
插入广告
使用 WordPress 短代码可以很方便的控制广告位置, 在文章任意位置显示广告将会显得十分方便. 以下以添加 Google Adsense 广告为例. 添加如下代码:

function advert_show( $atts ) {
extract(shortcode_atts(array('type' => 1), $atts));
switch ($type) {
case 1 :
$ad = '<script type="text/javascript">
google_ad_client = "ca-pub-3383777706744180";
/* 300 x 250 */
google_ad_slot = "2749227470";
google_ad_width = 300;
google_ad_height = 250;
//
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>';
break;
}
return $ad;
}
add_shortcode('advert', 'advert_show');

添加了一个 type 参数, 用于设置显示的广告类型, 通过 switch 的 case 判断, 如此可以设置多个广告, 只需添加相应 case 分支, 通过对 type 设置不同的值, 达到显示不同广告的效果. 在文章中插入代码

[advert]

即可插入广告, 由于 type 默认为 1, 所以会显示第一条广告, 如果添加了一个 case 分支到 2, 那么插入代码

[advert type=2]

即可显示第二条广告.

支付宝打赏微信打赏

如果此文对你有帮助,欢迎打赏作者。

发表评论

欢迎回来 (打开)

(必填)