How do I let a user input a video url and have the

2019-09-05 15:43发布

I've seen a few similar questions, but none of the answers have worked for me. Ideally, the user would input the video url into the form, hit submit, and then the video would show below the form. Here's my code that's not working (sorry if it's messy or confusing):

<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;
    }

?>

1条回答
疯言疯语
2楼-- · 2019-09-05 16:17

It appears that you are asking a user to enter a URL such as "www.youtube.com/watch?v=vidID", then taking that entire string value and inserting it into the <iframe> source value.

This essentially sets the source to "www.youtube.com/embed/www.youtube.com/watch?v=vidID".

You need to parse the URL submitted by the user and take only the vidID value before inserting into the <iframe>

Because of the structure of these URL's the video ID is the last value in the string. This means you could do "explode" the string and take the last value in the array that creates.

Here's a quick example of how you could do this:

<?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));
?>
查看更多
登录 后发表回答