Skip to content

Commit 102b5bc

Browse files
committed
Taxonomy: Add UI to Category page to indicate default category.
Shows the default category first in the list and adds a "Default" label next to its name in the categories admin screen. See https://core.trac.wordpress.org/ticket/26268
1 parent dc62ecb commit 102b5bc

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/wp-admin/includes/class-wp-terms-list-table.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,20 +242,40 @@ public function display_rows_or_placeholder() {
242242
return;
243243
}
244244

245+
// Handle custom display of default category by showing it first in the list.
246+
$default_category = false;
247+
if ( 'category' === $taxonomy ) {
248+
$default_category = get_term( get_option( 'default_category' ), 'category' );
249+
if ( ! $default_category || is_wp_error( $default_category ) ) {
250+
$default_category = false;
251+
}
252+
}
253+
245254
if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $this->callback_args['orderby'] ) ) {
246255
if ( ! empty( $this->callback_args['search'] ) ) {// Ignore children on searches.
247256
$children = array();
248257
} else {
249258
$children = _get_term_hierarchy( $taxonomy );
250259
}
251260

261+
if ( $default_category ) {
262+
$this->single_row( $default_category );
263+
}
264+
252265
/*
253266
* Some funky recursion to get the job done (paging & parents mainly) is contained within.
254267
* Skip it for non-hierarchical taxonomies for performance sake.
255268
*/
256269
$this->_rows( $taxonomy, $this->items, $children, $offset, $number, $count );
257270
} else {
271+
if ( $default_category ) {
272+
$this->single_row( $default_category );
273+
}
274+
258275
foreach ( $this->items as $term ) {
276+
if ( $default_category && $default_category->term_id === $term->term_id ) {
277+
continue;
278+
}
259279
$this->single_row( $term );
260280
}
261281
}
@@ -275,6 +295,12 @@ private function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$coun
275295

276296
$end = $start + $per_page;
277297

298+
// Handle display of default category by showing it first in the list, capture default category id.
299+
$default_category_id = false;
300+
if ( 'category' === $taxonomy ) {
301+
$default_category_id = (int) get_option( 'default_category' );
302+
}
303+
278304
foreach ( $terms as $key => $term ) {
279305

280306
if ( $count >= $end ) {
@@ -285,6 +311,11 @@ private function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$coun
285311
continue;
286312
}
287313

314+
// Skip duplicating display of default category.
315+
if ( $default_category_id && $default_category_id === $term->term_id ) {
316+
continue;
317+
}
318+
288319
// If the page starts in a subtree, print the parents.
289320
if ( $count === $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
290321
$my_parents = array();
@@ -383,6 +414,12 @@ public function column_cb( $item ) {
383414
public function column_name( $tag ) {
384415
$taxonomy = $this->screen->taxonomy;
385416

417+
$default_term = get_option( 'default_' . $taxonomy );
418+
$default_term_label = '';
419+
if ( $tag->term_id == $default_term ) {
420+
$default_term_label = ' &mdash; <span class="taxonomy-default-label">' . __( 'Default' ) . '</span>';
421+
}
422+
386423
$pad = str_repeat( '&#8212; ', max( 0, $this->level ) );
387424

388425
/**
@@ -422,8 +459,9 @@ public function column_name( $tag ) {
422459
}
423460

424461
$output = sprintf(
425-
'<strong>%s</strong><br />',
426-
$name
462+
'<strong>%s%s</strong><br />',
463+
$name,
464+
$default_term_label
427465
);
428466

429467
/** This filter is documented in wp-admin/includes/class-wp-terms-list-table.php */

0 commit comments

Comments
 (0)