php - How do I let a user input a video url and have the video show on the same page upon submit? -
i've seen few similar questions, none of answers have worked me. ideally, user input video url form, hit submit, , video show below form. here's 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'])) { // video url , put in $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 found').'</span>'; } } else{ $result = '<span style="color:red;">'._e('the video source either not set or not supported').'.</span>'; } return $result; } ?>
it appears asking user enter url such "www.youtube.com/watch?v=vidid", taking entire string value , inserting <iframe>
source value.
this sets source "www.youtube.com/embed/www.youtube.com/watch?v=vidid".
you need parse url submitted user , take vidid value before inserting <iframe>
because of structure of these url's video id last value in string. means "explode" string , take last value in array creates.
here's quick example of how 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 video source switch ($videosource) { case 'youtube': $delimiter = '='; break; case 'vimeo': $delimiter = '/'; break; default: //whatever fallback want } //isolate vidid value string $vidid = end(explode($delimiter, $vidurl)); ?>
Comments
Post a Comment