简体   繁体   English

包括wp_enqueue_media的问题-WordPress

[英]issue including wp_enqueue_media - WordPress

I am building a custom interface that is utilizing WordPress functions and a specific area I am trying to utilize is the media section of WordPress. 我正在构建一个利用WordPress功能的自定义界面,而我尝试利用的特定区域是WordPress的媒体部分。 I am first including the core files of WordPress: 我首先包括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');
}

Than within my media file I am using: wp_enqueue_media() to allow me to access the media viewer for when a user is uploading media from their account. 在我的媒体文件中,我使用的是: wp_enqueue_media() ,以便当用户从其帐户上传媒体时允许我访问媒体查看器。

My JS script I am using to run the media request is as follows: 我用来运行媒体请求的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();
});

The issue here is, when running the add-media event, wp is undefined. 这里的问题是,在运行add-media事件时, wp是未定义的。 Now I am not sure as to why this is. 现在我不确定为什么会这样。 Any thoughts or suggestions? 有什么想法或建议吗?

After looking through documentation, I couldn't really figure out why this was happening. 浏览文档后,我无法真正弄清楚为什么会这样。 But I found the solution. 但是我找到了解决方案。 Some final files are called when using wp_footer() . 使用wp_footer()时会调用一些最终文件。 And without this, none of your JavaScript files will run. 否则,您的JavaScript文件将不会运行。

So In the future if your developing an application and want to use WordPress core functionalities, make sure to include wp_footer() at the end of your application to make sure you won't run into wp not being defined in js . 因此,将来如果您开发应用程序并希望使用WordPress核心功能,请确保在应用程序末尾包含wp_footer() ,以确保不会遇到未在js中定义的wp情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM