• Resolved annarama

    (@annarama)


    Hello there,

    Let’s see if i can explain myself in english and hope that I am in the right forum for this question.

    In my post i have three Custom Fields (wpcf-video_embed_trailer, wpcf-video_embed_extras, wpcf-video_embed_example). Each field can contain more than one link. I want to show the links from the arrays embedded in my sidebar. My Company wants to be able to put youtube and vimeo links there.

    I already found a piece of code and altered it, so that the ID from the first key gets extracted and inserted into a shortcode in the sidebar which then embeds the video as i please ;). This works just fine for a one key. But i want every key with their values displayed in the sidebar.

    I know that i need a loop with foreach. But i don’t know how to approach this, for not one custom field array but for three. Not quite enough PHP knowledge (wordpress hooks are all i know) for this.

    Can anyone give me guidance or a hint how to achieve this?
    I can’t give you the link for my page because of company security.

    <?php
    // Check if youtube or Vimeo and Extract the IDS. Displayed with Shortcode in Sidebar
    global $wp_query;
    $postid = $wp_query->post->ID;
    
    $wpcf_video = get_post_meta($postid, 'wpcf-video_embed_trailer', true);
    
    $parsed     = parse_url($wpcf_video);
    $hostname   = $parsed['host'];
    $query      = $parsed['query'];
    $path       = $parsed['path']; 
    
    //print_r($parsed);
    
    $Arr = explode('v=',$query);
    // from video id, until to end of the string
    $videoIDwithString = $Arr[1];
    
    $videoID = substr($videoIDwithString,0,11);
    //print_r($videoID);
    
    $yt1 = '[youtube id="';
    $yt2 = '" mode="thumbnail"]';
    
    //YOUTUBE.COM
    if( (isset($videoID)) && (isset($hostname)) && ($hostname=='www.youtube.com' || $hostname=='youtube.com')){
    
    ?>
    
    <?php echo do_shortcode($yt1.=$videoID.=$yt2); ?>
    
    <?php
    }
    ?>
    
    <?php
    $v1 = '[vimeo id="';
    $v2 = '" mode="thumbnail"]';
    
    //VIMEO.COM
    //https://vimeo.com/45980982
    if((isset($hostname)) && $hostname=='vimeo.com'){
        $ArrV = explode('://vimeo.com/',$path); // from ID to end of the string
        $videoID = substr($ArrV[0],1,8); // to get video ID
        $vimdeoIDInt = intval($videoID); // ID must be integer
        //print_r($vimdeoIDInt);
    ?>
    <?php echo do_shortcode($v1.=$vimdeoIDInt.=$v2); ?>
    <?php
    }
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • It may be simpler to just use a plugin like YOUTUBE CHANNEL to do what you want.

    Thread Starter annarama

    (@annarama)

    Sry. It doesn’t. Our employees must be able to insert just a link from Youtube OR Vimeo. Also the structure for handling the related video to that post is different compared to how we handle our youtube playlists. I have already searched for two days straight for a plugin that combines what i want to achieve (automatically insert embedded videos from three custom field arrays, open in colorbox, related to special taxonomy terms etc.) And I don’t want to handle 10 Plugins just for this. So i decided to hardcode this into the sidebar. Maybe someone can suggest a plugin that does exactly that, at least I haven’t found the right one.

    Thread Starter annarama

    (@annarama)

    I figured it out. Here is my Code.

    <?php
    // Check if youtube or Vimeo and Extract the IDS. Displayed with Shortcode in Sidebar
    global $wp_query;
    $postid = $wp_query->post->ID;
    
    $wpcf_video_trailers = get_post_meta($postid, 'wpcf-video_embed_trailer', false);
    $wpcf_video_extras = get_post_meta($postid, 'wpcf-video_embed_extras', false);
    $wpcf_video_drills = get_post_meta($postid, 'wpcf-video_embed_drills', false);
    $wpcf_videos = array_merge ($wpcf_video_trailers,$wpcf_video_extras,$wpcf_video_drills);
    
    foreach ($wpcf_videos as $wpcf_video) {
    		$parseds[]    = parse_url($wpcf_video);
    	}
    foreach ($parseds as $parsed) {
    		$hostnames[]   = $parsed['host'];
    		$querys[]      = $parsed['query'];
    		$paths[]       = $parsed['path'];
    	}
    
    	if ($hostname=='www.youtube.com' || $hostname=='youtube.com');
    		foreach ($querys as $query){
    			$Arr = explode('v=',$query);
    			$videoIDwithString = $Arr[1];
    			$videoIDsf[] = substr($videoIDwithString,0,11);
    		}
    $videoIDs = array_filter($videoIDsf);
    				//YOUTUBE.COM
    					if(isset($videoIDs)){
    						foreach ($videoIDs as $videoID)
    					echo do_shortcode('[youtube id="'.$videoID.'" mode="thumbnail"]');
    					}
    	if ($hostname=='vimeo.com');
    		foreach ($paths as $path){
    				$ArrV = explode('://vimeo.com/',$path);
    				$vimeoID = substr($ArrV[0],1,8);
    				$vimeoIDsIntf[] = intval($vimeoID);
    		}
    $vimeoIDsInt = array_filter($vimeoIDsIntf);
    				//VIMEO.COM
    					if(isset($vimeoIDsInt)){
    						foreach ($vimeoIDsInt as $vimeoIDInt)
    					echo do_shortcode('[vimeo id="'.$vimeoIDInt.'" mode="thumbnail"]');
    					}
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Embed Vimeo/youtube Video as Loop in Sidebar with Values of a Custom Field Array’ is closed to new replies.