0

古いコンテンツに次のようなショートコードがある方法で、ワードプレスのテーマのコーディングを開始しました。

[youtube]http://www.youtube.com/watch?v=4MhsnuHvZoI[/youtube]

ただし、このコードはプラグインを使用して自動埋め込み領域を生成します。実際、新しいプロジェクトでプラグインを使用したくありません。このため、私はこのような単純なshourtcodeを作成しました;

<?php

    //  YoutubeEmbed

    function youtubekod($atts,$content = null ) {
    return '<iframe width="100%" height="315" src="http://www.youtube.com/embed/'.$content.'?rel=0" frameborder="0" allowfullscreen></iframe>'; 
    }
    add_shortcode('youtube', 'youtubekod');


?>

ただし、ショートコードの戻り行を確認すると、あなたは私がやろうとしたことを見るでしょう。ただし、問題は、 [youtube]タグ間でビデオIDだけを取得するにはどうすればよいかということです。私はあなたの助けをうれしく思います。

ありがとう。

4

2 に答える 2

0

これを試して

function youtubekod($atts, $content="") {
    parse_str( parse_url( $content, PHP_URL_QUERY ), $youtube_id ); 
    return '<iframe width="100%" height="315" src="http://www.youtube.com/embed/'.$youtube_id[v].'?rel=0" frameborder="0" allowfullscreen></iframe>'; 
}
add_shortcode('youtube', 'youtubekod');
于 2013-02-08T06:32:12.083 に答える
0

これを試して。

<?php

    //  YoutubeEmbed

    function youtubekod($atts,$content = null ) {

        $url = get_the_content($post->post_content);
        $id=0;
        preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $matches);
        if(isset($matches[1])) $id = $matches[1];
        if(!$id) {
            $matches = explode('/', $url);
            $id = $matches[count($matches)-1];
        }
        $code = '<iframe width="100%" height="315" src="http://www.youtube.com/embed/{id}?rel=0" frameborder="0" allowfullscreen></iframe>';

        $code = str_replace('{id}', $id, $code);

        return $code;

    }
    add_shortcode('youtube', 'youtubekod');

?>
于 2013-02-08T12:49:07.403 に答える