Introducing WP_List_Table::get_views_links() in WordPress 6.1

The Problem

Previously, the code to generate markup for views links had to be added in the get_views() method of each child class. This led to repetitive and inconsistent code to achieve the same result, increasing the maintenance burden for CoreCore Core is the set of software required to run WordPress. The Core Development Team builds WordPress. and extenders alike.

The Solution

A new method was proposed in ticketticket Created for both bug reports and feature development on the bug tracker. #42066, WP_List_Table::get_views_links(), which abstracts the link generation to the parent class.

This new protected method accepts a $link_data array argument containing the following for each view:

  • $url (string) The link URLURL A specific web address of a website or web page on the Internet, such as a website’s URL www.wordpress.org.
  • $label (string) The link label.
  • $current (bool) Optional. Whether this is the currently selected view.

If $current is true, aria-current="page" and class="current" will be added to the item’s link markup.

Usage

  1. In the get_views() method, create an array of link data as documented above.
  2. Pass the array to the new method.
class My_List_Table extends WP_List_Table {
	protected function get_views() {
		// Create link data.
		$base_url  = add_query_arg( 'post_type', 'my_cpt', admin_url( 'edit.php' ) );
		$link_data = array(
			'all' => array(
				'url'     => $base_url,
				'label'   => __( 'All', 'my_textdomain' ),
				'current' => true, // Optional.
			),
			'trash' => array(
				'url'   => add_query_arg( 'post_status', 'trash', $base_url ),
				'label' => __( 'Trash', 'my_text_domain' ),
			),
		);

		// Generate link markup.
		return $this->get_views_links( $link_data );
	}
}

Core Child Class Updates

The following Core child classes now use the new method:

Thanks to @davidbaumwald for peer review.

#6-1, #dev-notes, #dev-notes-6-1