2011-12-09 2 views
1

내 워드 프레스 설치의 "비디오"디렉토리에 많은 비디오가 있습니다.mediaelement.js - 임의의 동영상을 재생 하시겠습니까?

그들은 모두 MediaElement.js 플러그인을 사용하여 훌륭하게 재생되지만이 디렉토리에서 임의의 클립을 재생할 수도 있습니까? 예를 들어 디렉터리로 연결되는 단축키를 사용하면 다음과 같은 내용이 표시됩니다.

[video src="http://www.domain.com/wordpress/wp-content/video" random="true"] 

이렇게 좋을 것입니다!

답변

1

가능해야합니다.

AJAX를 사용하여 동영상 플레이어가 포함 된 div를 생성하는 것이 좋습니다. 이렇게하면 플레이어를 아주 쉽게 삭제/다시 만들 수 있습니다.

그런 다음 단축 코드 처리기에 첨부하는 함수에 디렉토리 문자열 값과 부울 값을 제공하는 단축 코드 정의가 필요합니다.

$ defaultDirectory = site_url() + "/ 비디오 /"예를 들어

; http://php.net/manual/en/function.readdir.php

<?php 
/** 
* get the number of files within a given dir 
*/ 
function fileCounter($dir){ 
    $counter = 0; 
    if ($handle = opendir($dir)) { 
     //echo "Directory handle: $handle\n"; 
     //echo "Files:\n"; 
     /* This is the correct way to loop over the directory. */ 
     while (false !== ($file = readdir($handle))) { 
      //echo "<BR>".$counter." - $file"; 
      $counter++; 
     } 
     closedir($handle); 
    } 
    $counter -= 1; // in order to exclude '.' and '..', as well as start the counter on 1 
    return $counter; 
} 
/** 
* get the filename in a giver dir, starting the first file with the index 1 
*/ 
function fileNameByIndex($dir, $fileIndex){ 
    $counter = 0; 
    if ($handle = opendir($dir)) { 
     while (false !== ($file = readdir($handle))) { 
      $counter++; 
      if($counter - 2 == $fileIndex) 
       return $file; 
     } 
     closedir($handle); 
    } 
    return ""; 
} 
}  
에서도

add_shortcode('video', 'videoDiv'); 

function videoDiv($shortcodeAttributeList) 
{ 
    extract(shortcode_atts(array(
      'src' => $defaultDirectory, 
      'random' => true,    /*set default values, use lowercase*/ 
     ), $shortcodeAttributeList)); 

    if($random) 
    { 
     $numFiles=fileCounter($src); 
     $choice=rand(1, $numFiles); 
    } 

    $output='<div id="videoPlayer" class="player">'; 
    // Continue by making a string which spans from <div> to </div> 

    return $output; //a div 
}