我见过一些类似的问题,但没有一个答案适合我。理想情况下,用户将在表单中输入视频url,点击提交,然后视频将显示在表单下方。这是我的代码不工作(对不起,如果它是混乱或混淆):
<form id="rp_embed_video" name="rp_embed-video" method="post" action="">
<div class="rp_block">
<label><?php printf( __( 'Add Video Link:' ) ); ?></label>
<input type="url" id="rp_newvid" name="rp_newvid" value="" required />
</div>
<div class="rp_block">
<label><?php printf( __( 'Choose One:' ) );?></label>
<input type="radio" name="rp_type" value="YouTube" required checked />
<input type="radio" name="rp_type" value="Vimeo" required />
</div>
<div class="rp_block">
<label><?php _e('Title');?></label>
<input type="text" name="rp_title" value="" />
</div>
<div class="rp_block">
<?php $desc_status = (int)get_option('rp_uploader_desc',false);?>
<label><?php _e('Description');?><?php if(!$desc_status):?><span>*</span><?php endif;?></label>
<textarea name="rp_desc" <?php if(!$desc_status):?>class="wpvp_require"<?php endif;?>></textarea>
</div>
<div class="rp_block">
<div class="rp_cat" style="float:left;width:50%;">
<label><?php _e('Choose category');?></label>
<?php RP_Helper::rp_upload_categories_dropdown();?>
</div>
<?php
$hide_tags = get_option('rp_uploader_tags','');
if($hide_tags==''){ ?>
<div class="rp_tag" style="float:right;width:50%;text-align:right;">
<label><?php _e('Tags (comma separated)');?></label>
<input type="text" name="rp_tags" value="" />
</div>
<?php
}
?>
</div>
<input type="hidden" name="rp_action" value="rp_embed" />
<p class="rp_submit_block">
<input type="submit" class="rp-submit" name="rp-embed" value="Add Video" />
</p>
</form>
<?php
if (isset($_POST['rp_newvid'])) {
// Get the video URL and put it in the $video variable
$videoID = $_POST['rp_newvid'];
$type = $_POST['rp_type'];
echo rp_video_embed($_POST['rp_newvid'], '720px', '380px', $_POST['rp_type']);
}
?>
<?php
function rp_video_embed($videoID,$width,$height,$type){
if($type){
if($videoID){
if($type=='YouTube'){
$embedCode = '<iframe width="'.$width.'" height="'.$height.'" src="http://www.youtube.com/embed/'.$videoID.'" frameborder="0" allowfullscreen></iframe>';
}
elseif($type=='Vimeo'){
$embedCode = '<iframe width="'.$width.'" height="'.$height.'" src="http://player.vimeo.com/video/'.$videoID.'" webkitAllowFullScreen mozallowfullscreen allowFullScreen frameborder="0"></iframe>';
}
$result = $embedCode;
}
else{
$result = '<span style="color:red;">'._e('No video code is found').'</span>';
}
}
else{
$result = '<span style="color:red;">'._e('The video source is either not set or is not supported').'.</span>';
}
return $result;
}
?>
看起来您正在要求用户输入URL,例如"www.youtube.com/watch?v=vidID",然后将整个字符串值插入<iframe>
源值。
这实际上是将源设置为"www.youtube.com/embed/www.youtube.com/watch?v=vidID"。
您需要解析用户提交的URL,在插入<iframe>
之前只取vidID值
由于这些URL的结构,视频ID是字符串中的最后一个值。这意味着你可以对字符串进行"爆炸",并取数组中创建的最后一个值。
下面是一个快速示例,说明如何做到这一点:
<?php
//Sample Variables
$youtubeURL = 'www.youtube.com/watch?v=vidID';
$vimeoURL = 'https://vimeo.com/vidID';
//Test Values
$vidURL = $youtubeURL;
$videoSource = 'youtube';
//Select Delimiter based on the video source
switch ($videoSource) {
case 'youtube':
$delimiter = '=';
break;
case 'vimeo':
$delimiter = '/';
break;
default:
//Whatever fallback you want
}
//Isolate the vidID value string
$vidID = end(explode($delimiter, $vidURL));
?>