wp list table - Modify WP_List_Table pagination "item(s)" text?

I'm developing a plugin which creates several custom tables on pages in the WP dashboard. My question is whether it

I'm developing a plugin which creates several custom tables on pages in the WP dashboard. My question is whether it's possible (without modifying core files) to change the text displayed in the pagination above those tables? Specifically, I'd like to change the word "item/items" to reflect the object type being displayed in the table.

I've looked at the core file which defines the WP_List_Table class to see where the method defines that text, but it doesn't seem you can pass any arguments to modify it when you extend the class and pass the pagination params...

Here's the exact portion of the code that defines the text being displayed in the image above:

$output = '<span class="displaying-num">' . sprintf( _n( '%s item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';

And below is the full method for context reference...

/**
 * Display the pagination.
 *
 * @since 3.1.0
 *
 * @param string $which
 */
protected function pagination( $which ) {
  if ( empty( $this->_pagination_args ) ) {
    return;
  }

  $total_items     = $this->_pagination_args['total_items'];
  $total_pages     = $this->_pagination_args['total_pages'];
  $infinite_scroll = false;
  if ( isset( $this->_pagination_args['infinite_scroll'] ) ) {
    $infinite_scroll = $this->_pagination_args['infinite_scroll'];
  }

  if ( 'top' === $which && $total_pages > 1 ) {
    $this->screen->render_screen_reader_content( 'heading_pagination' );
  }

  $output = '<span class="displaying-num">' . sprintf( _n( '%s item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';

  $current = $this->get_pagenum();
  $removable_query_args = wp_removable_query_args();

  $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );

  $current_url = remove_query_arg( $removable_query_args, $current_url );

  $page_links = array();

  $total_pages_before = '<span class="paging-input">';
  $total_pages_after  = '</span></span>';

  $disable_first = $disable_last = $disable_prev = $disable_next = false;

  if ( $current == 1 ) {
    $disable_first = true;
    $disable_prev  = true;
  }
  if ( $current == 2 ) {
    $disable_first = true;
  }
  if ( $current == $total_pages ) {
    $disable_last = true;
    $disable_next = true;
  }
  if ( $current == $total_pages - 1 ) {
    $disable_last = true;
  }

  if ( $disable_first ) {
    $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&laquo;</span>';
  } else {
    $page_links[] = sprintf(
      "<a class='first-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
      esc_url( remove_query_arg( 'paged', $current_url ) ),
      __( 'First page' ),
      '&laquo;'
    );
  }

  if ( $disable_prev ) {
    $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&lsaquo;</span>';
  } else {
    $page_links[] = sprintf(
      "<a class='prev-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
      esc_url( add_query_arg( 'paged', max( 1, $current - 1 ), $current_url ) ),
      __( 'Previous page' ),
      '&lsaquo;'
    );
  }

  if ( 'bottom' === $which ) {
    $html_current_page  = $current;
    $total_pages_before = '<span class="screen-reader-text">' . __( 'Current Page' ) . '</span><span id="table-paging" class="paging-input"><span class="tablenav-paging-text">';
  } else {
    $html_current_page = sprintf(
      "%s<input class='current-page' id='current-page-selector' type='text' name='paged' value='%s' size='%d' aria-describedby='table-paging' /><span class='tablenav-paging-text'>",
      '<label for="current-page-selector" class="screen-reader-text">' . __( 'Current Page' ) . '</label>',
      $current,
      strlen( $total_pages )
    );
  }
  $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
  $page_links[]     = $total_pages_before . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . $total_pages_after;

  if ( $disable_next ) {
    $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&rsaquo;</span>';
  } else {
    $page_links[] = sprintf(
      "<a class='next-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
      esc_url( add_query_arg( 'paged', min( $total_pages, $current + 1 ), $current_url ) ),
      __( 'Next page' ),
      '&rsaquo;'
    );
  }

  if ( $disable_last ) {
    $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&raquo;</span>';
  } else {
    $page_links[] = sprintf(
      "<a class='last-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
      esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
      __( 'Last page' ),
      '&raquo;'
    );
  }

  $pagination_links_class = 'pagination-links';
  if ( ! empty( $infinite_scroll ) ) {
    $pagination_links_class .= ' hide-if-js';
  }
  $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';

  if ( $total_pages ) {
    $page_class = $total_pages < 2 ? ' one-page' : '';
  } else {
    $page_class = ' no-pages';
  }
  $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";

  echo $this->_pagination;
}

I'm developing a plugin which creates several custom tables on pages in the WP dashboard. My question is whether it's possible (without modifying core files) to change the text displayed in the pagination above those tables? Specifically, I'd like to change the word "item/items" to reflect the object type being displayed in the table.

I've looked at the core file which defines the WP_List_Table class to see where the method defines that text, but it doesn't seem you can pass any arguments to modify it when you extend the class and pass the pagination params...

Here's the exact portion of the code that defines the text being displayed in the image above:

$output = '<span class="displaying-num">' . sprintf( _n( '%s item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';

And below is the full method for context reference...

/**
 * Display the pagination.
 *
 * @since 3.1.0
 *
 * @param string $which
 */
protected function pagination( $which ) {
  if ( empty( $this->_pagination_args ) ) {
    return;
  }

  $total_items     = $this->_pagination_args['total_items'];
  $total_pages     = $this->_pagination_args['total_pages'];
  $infinite_scroll = false;
  if ( isset( $this->_pagination_args['infinite_scroll'] ) ) {
    $infinite_scroll = $this->_pagination_args['infinite_scroll'];
  }

  if ( 'top' === $which && $total_pages > 1 ) {
    $this->screen->render_screen_reader_content( 'heading_pagination' );
  }

  $output = '<span class="displaying-num">' . sprintf( _n( '%s item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';

  $current = $this->get_pagenum();
  $removable_query_args = wp_removable_query_args();

  $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );

  $current_url = remove_query_arg( $removable_query_args, $current_url );

  $page_links = array();

  $total_pages_before = '<span class="paging-input">';
  $total_pages_after  = '</span></span>';

  $disable_first = $disable_last = $disable_prev = $disable_next = false;

  if ( $current == 1 ) {
    $disable_first = true;
    $disable_prev  = true;
  }
  if ( $current == 2 ) {
    $disable_first = true;
  }
  if ( $current == $total_pages ) {
    $disable_last = true;
    $disable_next = true;
  }
  if ( $current == $total_pages - 1 ) {
    $disable_last = true;
  }

  if ( $disable_first ) {
    $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&laquo;</span>';
  } else {
    $page_links[] = sprintf(
      "<a class='first-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
      esc_url( remove_query_arg( 'paged', $current_url ) ),
      __( 'First page' ),
      '&laquo;'
    );
  }

  if ( $disable_prev ) {
    $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&lsaquo;</span>';
  } else {
    $page_links[] = sprintf(
      "<a class='prev-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
      esc_url( add_query_arg( 'paged', max( 1, $current - 1 ), $current_url ) ),
      __( 'Previous page' ),
      '&lsaquo;'
    );
  }

  if ( 'bottom' === $which ) {
    $html_current_page  = $current;
    $total_pages_before = '<span class="screen-reader-text">' . __( 'Current Page' ) . '</span><span id="table-paging" class="paging-input"><span class="tablenav-paging-text">';
  } else {
    $html_current_page = sprintf(
      "%s<input class='current-page' id='current-page-selector' type='text' name='paged' value='%s' size='%d' aria-describedby='table-paging' /><span class='tablenav-paging-text'>",
      '<label for="current-page-selector" class="screen-reader-text">' . __( 'Current Page' ) . '</label>',
      $current,
      strlen( $total_pages )
    );
  }
  $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
  $page_links[]     = $total_pages_before . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . $total_pages_after;

  if ( $disable_next ) {
    $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&rsaquo;</span>';
  } else {
    $page_links[] = sprintf(
      "<a class='next-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
      esc_url( add_query_arg( 'paged', min( $total_pages, $current + 1 ), $current_url ) ),
      __( 'Next page' ),
      '&rsaquo;'
    );
  }

  if ( $disable_last ) {
    $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&raquo;</span>';
  } else {
    $page_links[] = sprintf(
      "<a class='last-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
      esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
      __( 'Last page' ),
      '&raquo;'
    );
  }

  $pagination_links_class = 'pagination-links';
  if ( ! empty( $infinite_scroll ) ) {
    $pagination_links_class .= ' hide-if-js';
  }
  $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';

  if ( $total_pages ) {
    $page_class = $total_pages < 2 ? ' one-page' : '';
  } else {
    $page_class = ' no-pages';
  }
  $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";

  echo $this->_pagination;
}
Share Improve this question edited May 30, 2019 at 7:16 Josh Jones asked May 29, 2019 at 4:58 Josh JonesJosh Jones 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

So I just copied the pagination method to my class and changed the wording within it. I would've imagined WP would have a more elegant solution, but apparently not.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745452208a4628322.html

相关推荐

  • wp list table - Modify WP_List_Table pagination &quot;item(s)&quot; text?

    I'm developing a plugin which creates several custom tables on pages in the WP dashboard. My question is whether it

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信