php 功能 - 管理媒体库 - 添加可排序的大小列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 功能 - 管理媒体库 - 添加可排序的大小列相关的知识,希望对你有一定的参考价值。

<?php 

// For Step 1, I created a function that should only be run once. It’s a simple custom query that iterates through all attachments that don’t have filesize meta set yet. It’s commented out in my Gist because it can cause issues on your site if you have a large Media Library. Use it cautiously and use it once.


// Run only once, and then remove! - Adds initial meta to each attachment
function add_initial_file_size_postmeta($column_name, $post_id) {
  static $query_ran;
  if($query_ran !== null) {
    $all_imgs = new WP_Query(array(
      'post_type'      => 'attachment',
      'post_status'    => 'inherit',
      'posts_per_page' => -1,
      'fields'         => 'ids'
    ));
    $all_imgs_array = $all_imgs->posts;
    foreach($all_imgs_array as $a) {
      if(!get_post_meta($a, 'filesize', true)) {
        $file = get_attached_file($a);
        update_post_meta($a, 'filesize', filesize($file));
      }
    }
    $query_ran = true;
  }
}
add_action('manage_media_custom_column', __NAMESPACE__.'\\add_initial_file_size_postmeta');

?>
<?php

/*****************************************
Add sortable size column in Media Library
******************************************/
// Ensure file size meta gets added to new uploads
function add_filesize_metadata_to_images($meta_id, $post_id, $meta_key, $meta_value) {
  if('_wp_attachment_metadata' == $meta_key) {
    $file = get_attached_file($post_id);
    update_post_meta($post_id, 'filesize', filesize($file));
  }
}
add_action('added_post_meta', 'add_filesize_metadata_to_images', 10, 4);

// Add the column
function add_column_file_size($posts_columns) {
  $posts_columns['filesize'] = __('File Size');
  return $posts_columns;
}
add_filter('manage_media_columns', 'add_column_file_size');

// Populate the column
function add_column_value_file_size($column_name, $post_id) {
  if('filesize' == $column_name) {
    if(!get_post_meta($post_id, 'filesize', true)) {
      $file      = get_attached_file($post_id);
      $file_size = filesize($file);
      update_post_meta($post_id, 'filesize', $file_size);
    } else {
      $file_size = get_post_meta($post_id, 'filesize', true);
    }
    echo size_format($file_size, 2);
  }
  return false;
}
add_action('manage_media_custom_column', 'add_column_value_file_size', 10, 2);

// Make column sortable
function add_column_sortable_file_size($columns) {
  $columns['filesize'] = 'filesize';
  return $columns;
}
add_filter('manage_upload_sortable_columns', 'add_column_sortable_file_size');

// Column sorting logic (query modification)
function sortable_file_size_sorting_logic($query) {
  global $pagenow;
  if(is_admin() && 'upload.php' == $pagenow && $query->is_main_query() && !empty($_REQUEST['orderby']) && 'filesize' == $_REQUEST['orderby']) {
    $query->set('order', 'ASC');
    $query->set('orderby', 'meta_value_num');
    $query->set('meta_key', 'filesize');
    if('desc' == $_REQUEST['order']) {
      $query->set('order', 'DSC');
    }
  }
}
add_action('pre_get_posts', 'sortable_file_size_sorting_logic');

?>

以上是关于php 功能 - 管理媒体库 - 添加可排序的大小列的主要内容,如果未能解决你的问题,请参考以下文章

PHP 将可排序列添加到WordPress管理员

wordpress 媒体库图片无法显示?

php Gravity Perks // GP媒体库//自动添加文件ID的自定义字段

大家好!谷歌play 怎么添加本地音乐啊????

为啥 PHP 媒体库不起作用?

Python游戏开发常用库