the_alex_mark

Создание сортируемой колонки ID в админ-панели WordPress

Mar 7th, 2020
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1. // Создание колонки "ID"
  2. add_filter('manage_posts_columns', 'add_id_column', 5);
  3. add_filter('manage_pages_columns', 'add_id_column', 5);
  4. function add_id_column($columns) {
  5.     $out = array();
  6.     foreach ($columns as $col => $name) {
  7.         if (++$i == 3) $out['id'] = __('ID');
  8.         $out[$col] = $name;
  9.     }
  10.  
  11.     return $out;
  12. }
  13.  
  14. // Заполнение колонки "ID" данными
  15. add_filter('manage_posts_custom_column', 'fill_id_column', 5, 2);
  16. add_filter('manage_pages_custom_column', 'fill_id_column', 5, 2);
  17. function fill_id_column($colname, $post_id) {
  18.     if ($colname === 'id')
  19.         echo $post_id;
  20. }
  21.  
  22. // Правка ширины колонки "ID" через CSS
  23. add_action('admin_head', 'edit_id_column_css');
  24. function edit_id_column_css() {
  25.     if (get_current_screen()->base == 'edit')
  26.         echo '<style type="text/css"> .column-id { width: 10%; } </style>';
  27. }
  28.  
  29. // Сортировка колонки "ID"
  30. add_filter('manage_edit-post_sortable_columns', 'edit_id_sortable_column');
  31. add_filter('manage_edit-page_sortable_columns', 'edit_id_sortable_column');
  32. function edit_id_sortable_column($sortable_columns) {
  33.     $sortable_columns['id'] = 'id_id';
  34.     return $sortable_columns;
  35. }
Add Comment
Please, Sign In to add comment