使用$CONTENT的自定义YouTube快捷代码

时间:2013-01-08 作者:RogerNobrega

我想制作一个简单的快捷码,返回指定大小的youtube链接并使用内容。类似于:

[yt]http://youtu.be/Nl29v5pfxTw[/yt]
我有这个代码,但我想简化它:

    function youtube($atts) {
        extract(shortcode_atts(array(
                "value" => \'http://\',
                "width" => \'620\',
                "height" => \'350\',
                "name"=> \'movie\',
                "allowFullScreen" => \'true\',
                "allowScriptAccess"=>\'always\',
        ), $atts));
        return \'<object style="height: \'.$height.\'px; width: \'.$width.\'px"><param name="\'.$name.\'" value="\'.$value.\'"><param name="allowFullScreen" value="\'.$allowFullScreen.\'"></param><param name="allowScriptAccess" value="\'.$allowScriptAccess.\'"></param><embed src="\'.$value.\'" type="application/x-shockwave-flash" allowfullscreen="\'.$allowFullScreen.\'" allowScriptAccess="\'.$allowScriptAccess.\'" width="\'.$width.\'" height="\'.$height.\'"></embed></object>\';
}
add_shortcode("yt", "youtube");
问题是,对于这段代码,我需要使用[yt value=”http://youtu.be/Nl29v5pfxTw“]我只想使用[yt]$内容

我如何才能做到这一点?

谢谢

2 个回复
SO网友:shea

短代码函数接受second parameter 其中包含短代码开始标记和结束标记之间的值:

function youtube( $atts, $value = \'http://\' ) {
    extract( shortcode_atts( array(
        "width" => \'620\',
        "height" => \'350\',
        "name"=> \'movie\',
        "allowFullScreen" => \'true\',
        "allowScriptAccess"=>\'always\',
    ), $atts ) );
    return \'<object style="height: \'.$height.\'px; width: \'.$width.\'px"><param name="\'.$name.\'" value="\'.$value.\'"><param name="allowFullScreen" value="\'.$allowFullScreen.\'"></param><param name="allowScriptAccess" value="\'.$allowScriptAccess.\'"></param><embed src="\'.$value.\'" type="application/x-shockwave-flash" allowfullscreen="\'.$allowFullScreen.\'" allowScriptAccess="\'.$allowScriptAccess.\'" width="\'.$width.\'" height="\'.$height.\'"></embed></object>\';
}
add_shortcode( \'yt\', \'youtube\' );

SO网友:s_ha_dum

回调函数需要接受第二个参数。

function youtube($atts, $content) {
        extract(shortcode_atts(array(
                "value" => \'http://\',
                "width" => \'620\',
                "height" => \'350\',
                "name"=> \'movie\',
                "allowFullScreen" => \'true\',
                "allowScriptAccess"=>\'always\',
        ), $atts));
        return \'<object style="height: \'.$height.\'px; width: \'.$width.\'px"><param name="\'.$name.\'" value="\'.$content.\'"><param name="allowFullScreen" value="\'.$allowFullScreen.\'"></param><param name="allowScriptAccess" value="\'.$allowScriptAccess.\'"></param><embed src="\'.$content.\'" type="application/x-shockwave-flash" allowfullscreen="\'.$allowFullScreen.\'" allowScriptAccess="\'.$allowScriptAccess.\'" width="\'.$width.\'" height="\'.$height.\'"></embed></object>\';
}
我相信这是对的。There are examples in the Codex about how to do this.

结束

相关推荐

qtranslate arabic shortcode?

我在我的网站上使用了qTranslate。四种语言选择。English Russian Turkish Arabic 但阿拉伯语不行。我尝试了以下url:site.com/ar 但它仍然不起作用。显示如下错误:警告:无法修改标题信息-标题已由/home/useruser/public\\u html/www.site.com/blog/wp-content/themes/tema/index.php:1)在/home/useruser/public\\u html/www