te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - wp.media undefined using Wordpress Media Uploader - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - wp.media undefined using Wordpress Media Uploader - Stack Overflow

programmeradmin4浏览0评论

Edit: Other variations of scripts don't seem to work either, the wp_enqueue_media() goes alright, but it looks like the script that includes the wp.media is not included.

I'm trying to use the Wordpress Media Uploader in a custom plugin, but keep getting the following error:

TypeError: undefined is not an object (evaluating 'wp.media.frames')

My Javascript-code:

jQuery(document).ready(function(){

  var mediaUploader;

  jQuery('#upload-button').click(function(e) {
    e.preventDefault();
    // If the uploader object has already been created, reopen the dialog
      if (mediaUploader) {
      mediaUploader.open();
      return;
    }
    // Extend the wp.media object
    mediaUploader = wp.media.frames.file_frame = wp.media({
        title: 'Choose Image',
      button: {
      text: 'Choose Image'
    }, multiple: false });

    // When a file is selected, grab the URL and set it as the text field's value
    mediaUploader.on('select', function() {
      var attachment = mediaUploader.state().get('selection').first().toJSON();
      jQuery('#logo').val(attachment.url);
    });
    // Open the uploader dialog
    mediaUploader.open();
  });

});

The .js files are registered as follows:

/* Add the media uploader script */
  function my_media_lib_uploader_enqueue() {
    wp_enqueue_media();
    wp_register_script( 'media-lib-uploader-js', plugins_url( 'media-lib-uploader.js' , __FILE__ ), array('jquery') );
    wp_enqueue_script( 'media-lib-uploader-js' );
  }
  add_action('admin_enqueue_scripts', 'my_media_lib_uploader_enqueue');

Edit: Other variations of scripts don't seem to work either, the wp_enqueue_media() goes alright, but it looks like the script that includes the wp.media is not included.

I'm trying to use the Wordpress Media Uploader in a custom plugin, but keep getting the following error:

TypeError: undefined is not an object (evaluating 'wp.media.frames')

My Javascript-code:

jQuery(document).ready(function(){

  var mediaUploader;

  jQuery('#upload-button').click(function(e) {
    e.preventDefault();
    // If the uploader object has already been created, reopen the dialog
      if (mediaUploader) {
      mediaUploader.open();
      return;
    }
    // Extend the wp.media object
    mediaUploader = wp.media.frames.file_frame = wp.media({
        title: 'Choose Image',
      button: {
      text: 'Choose Image'
    }, multiple: false });

    // When a file is selected, grab the URL and set it as the text field's value
    mediaUploader.on('select', function() {
      var attachment = mediaUploader.state().get('selection').first().toJSON();
      jQuery('#logo').val(attachment.url);
    });
    // Open the uploader dialog
    mediaUploader.open();
  });

});

The .js files are registered as follows:

/* Add the media uploader script */
  function my_media_lib_uploader_enqueue() {
    wp_enqueue_media();
    wp_register_script( 'media-lib-uploader-js', plugins_url( 'media-lib-uploader.js' , __FILE__ ), array('jquery') );
    wp_enqueue_script( 'media-lib-uploader-js' );
  }
  add_action('admin_enqueue_scripts', 'my_media_lib_uploader_enqueue');
Share Improve this question edited Jan 28, 2017 at 15:34 Max asked Jan 21, 2017 at 13:50 MaxMax 8233 gold badges11 silver badges24 bronze badges 5
  • As a tip, you can save yourself time / effort by using the "no-conflict safe" document ready: jQuery(function($) { // inside here, you can use $ instead of typing jQuery, such as $('#upload-button').click....}); – random_user_name Commented Jan 21, 2017 at 14:31
  • 1 OK, to help, it would be useful to know when you get the error. Further, using debugging, you could also tell us where in your code the error ends up getting thrown. Finally, is this your first attempt? Did you ever have it working? If not, I'd strongly suggest following the example in the official WP docs: wp.media – random_user_name Commented Jan 21, 2017 at 14:35
  • Yes, I once had it working but suddenly it stopped. This was around the time that my site got updated to 4.7.1. The error occurs when I click the Upload button. The error is thrown one the line mediaUploader = wp.media.frames.file_frame = wp.media({ – Max Commented Jan 21, 2017 at 14:41
  • User your developer console to debug the script: set a pause point on that line, and then you can do things such as inspect / console.log the value of wp, and wp.media, etc. – random_user_name Commented Jan 21, 2017 at 14:43
  • Even when using the code from the official WP Docs I get the following error: TypeError: wp.media is not a function. (In 'wp.media', 'wp.media' is undefined).. – Max Commented Jan 21, 2017 at 14:52
Add a ment  | 

3 Answers 3

Reset to default 6

Solved the problem!

The problem was that wp_enqueue_media() calls the scripts into the footer of the page. Because I was using a die() function somewhere, the scripts weren't loaded.

put the wp_enqueue_media function in your enqueue scripts function.

Example:

add_action('wp_enqueue_scripts', 'prince_load_scripts');

function prince_load_scripts(){
wp_enqueue_media();
//Yor scripts goes here...
}

I also had a similiar problem... The scripts didn't load on one specific page, especially the scripts wp-media, which lead to the error "wp.media is not defined"... all the solutions didn't work. It turned out the content on this page was too massive, my PHP Memory Limit was to low. Setting up MemoryLimit 256->512 worked for me...

发布评论

评论列表(0)

  1. 暂无评论