2014-10-19 70 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定义。