最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding ?enablejsapi=1 to YouTube iframe Code - Stack Overflow

programmeradmin2浏览0评论

I have a Wordpress site and I have a custom field which holds YouTube iframe codes. There is a string holding these iframe codes named $embed. Videos are displayed in the theme with code:

<?php print($embed); ?>

I want to convert my standard YouTube embed codes such that:

Standard Youtube Embed Code:

<iframe width="560" height="315" src="" frameborder="0" allowfullscreen></iframe>

Converted Format:

<iframe width="560" height="315" src=";html5=1" frameborder="0" allowfullscreen id="video"></iframe>

Simply, I want to add ?enablejsapi=1&html5=1 to end of URL and add id="video".

How can i obtain this iframe code by manipulating the parameter $embed?

Tnx.

I have a Wordpress site and I have a custom field which holds YouTube iframe codes. There is a string holding these iframe codes named $embed. Videos are displayed in the theme with code:

<?php print($embed); ?>

I want to convert my standard YouTube embed codes such that:

Standard Youtube Embed Code:

<iframe width="560" height="315" src="https://www.youtube./embed/uxpDa-c-4Mc" frameborder="0" allowfullscreen></iframe>

Converted Format:

<iframe width="560" height="315" src="https://www.youtube./embed/uxpDa-c-4Mc?enablejsapi=1&html5=1" frameborder="0" allowfullscreen id="video"></iframe>

Simply, I want to add ?enablejsapi=1&html5=1 to end of URL and add id="video".

How can i obtain this iframe code by manipulating the parameter $embed?

Tnx.

Share Improve this question asked Feb 3, 2016 at 13:16 fatihtelisfatihtelis 171 gold badge1 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

This will add the extra params to the source. (Replace with your current print($embed) code.

// use preg_match to find iframe src
preg_match('/src="(.+?)"/', $embed, $matches);
$src = $matches[1];

// add extra params to iframe src
$params = array(
    'enablejsapi'=> 1,
    'html5' => 1
);

$new_src = add_query_arg($params, $src);
$embed = str_replace($src, $new_src, $embed);
$embed = str_replace('></iframe>', ' ' . $attributes . '></iframe>', $embed);

print($embed);
发布评论

评论列表(0)

  1. 暂无评论