2014-10-19 84 views
0

我正在構建一個自定義界面,利用WordPress的功能和我試圖利用的特定領域是WordPress的媒體部分。我第一次包括WordPress的核心文件:問題,包括wp_enqueue_media - WordPress

$location = $_SERVER['DOCUMENT_ROOT'] . "/wordpress"; 

include($location.'/wp-config.php'); 
include($location.'/wp-load.php'); 
include($location.'/wp-includes/pluggable.php'); 
global $wpdb; 
if(!isset($wpdb)) 
{ 
    include($location.'/wp-config.php'); 
    include($location.'/wp-includes/wp-db.php'); 
} 

比我的媒體文件中我使用:wp_enqueue_media()讓我訪問當用戶從他們的帳戶上傳媒體的媒體觀衆。

JS腳本我使用運行媒體請求如下:

$('.add-media').on('click', function(event){ 
    var file_frame; 
    var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id 
    event.preventDefault(); 

    // If the media frame already exists, reopen it. 
    if (file_frame) { 
     file_frame.open(); 
     return; 
    } 

    // Create the media frame. 
    file_frame = wp.media.frames.file_frame = wp.media({ 
     title: jQuery(this).data('uploader_title'), 
     button: { 
     text: jQuery(this).data('uploader_button_text'), 
     }, 
     multiple: true // Set to true to allow multiple files to be selected 
    }); 

    // When an image is selected, run a callback. 
    file_frame.on('select', function() { 

     var selection = file_frame.state().get('selection'); 

     selection.map(function(attachment) { 

      attachment = attachment.toJSON(); 
      // Do something with attachment.id and/or attachment.url here 

      $(".load-attachments").append('<div class="singleImg"><a href="'+attachment.url+'" class="shadowbox"><img src="'+attachment.url+'" width="100px" height="100px"/></a><input type="hidden" class="fileURL load-single-attachment" value="'+attachment.id+'"/><span class="removeImg remove"> X </span></div>'); 
      $(".removeImg").bind('click', function(){ 
      $(this).parent().remove(); 
      }); 

     }); 

    }); 

    // Finally, open the modal 
    file_frame.open(); 
}); 

問題這裏,運行add-media事件時,wp是不確定的。現在我不確定這是爲什麼。任何想法或建議?

回答

0

通過查看文檔後,我無法弄清楚爲什麼會發生這種情況。但我找到了解決方案。當使用wp_footer()時會調用一些最終文件。沒有這個,你的JavaScript文件都不會運行。

因此,在未來,如果你開發一個應用程序,並希望使用WordPress的核心功能,確保包括wp_footer()在應用程序的結束,以確保你不會碰到wp沒有在js定義。