WordPress.org

Ideas

WP_List_Table improvement (pagination and search)

  1. razvanm15
    Member

    WP Version 4.9, I have no way to control the pagination links that are rendered using the WPLT functionality, I currently have a search box, using that returns two pages, going to the next page resets the search because the links do not contain the search term.
    It would be nice to have a way to add custom query arguments to the link's rendered in the paging control

    WP_List_Table

    /**
     * Control the pagination link and arguments
     *
     * @param string $which - What button / link we are rendering (first, next, prev, last)
     * @param string $url   - The link for the current button
     * @param number $total - The total pages we have
     * @param number $current - The current page we are on
     */
    protected function pagination_render_args($which, $url, $total, $current){
    	if ($which === 'first'){
    		return remove_query_arg( 'paged', $url );
    	}else if ($which === 'prev'){
    		return add_query_arg( 'paged', max( 1, $current-1 ), $url );
    	}else if ($which === 'next'){
    		return add_query_arg( 'paged', min( $total, $current+1 ), $url );
    	}else if ($which === 'last'){
    		return add_query_arg( 'paged', $total, $url );
    	}else return $url;
    }
    
    ........................
    protected function pagination( $which ) {
    ................
    	if ( $disable_next ) {
    		$page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">›</span>';
    	} else {
    		$page_links[] = sprintf( "<a class='next-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
    			esc_url( $this->pagination_render_args('next', $current_url, $total_pages, $current) ),
    			__( 'Next page' ),
    			'›'
    		);
    	}

    Custom Admin List table

    function pagination_render_args($which, $url, $total, $current){
    	if (!empty($_REQUEST['s'])){
    		$url = add_query_arg('s', $_REQUEST['s'], $url);
    	}else{
    		$url = remove_query_arg('s', $url);
    	}
    	return parent::pagination_render_args($which, $url, $total, $current);
    }

    Rendering

    $ptable->prepare_items();
    $ptable->search_box('Search', 'search');
    $ptable->display();

    Now we can have search with paging, also support for other custom parameters in requests

    Posted: 9 months ago #
  2. Ipstenu (Mika Epstein)
    Administrator

    I currently have a search box, using that returns two pages, going to the next page resets the search because the links do not contain the search term.

    Then something is broken on your side because by default, that works just fine

    Posted: 9 months ago #
  3. razvanm15
    Member

    It works for GET forms not for POST

    Works

    <form method="get">
    	<?php
    		<input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
    		$ptable->search_box('Search', 'search');
    		$ptable->display();
    	?>
     </form>

    Does not work

    <form method="post">
    	<?php
    		$ptable->search_box('Search', 'search');
    		$ptable->display();
    	?>
     </form>

    I understand that doing a POST might not be the best method or the 'correct' method, it does not matter, a complete framework should allow you to do what you need. I still think that allowing custom URL rendering in pagination will improve the overall functionality, it's also not a big / breaking change.
    For example: A situation where I need statistics on what the user clicked so I can improve the search, this implies different params for each rendered link, I can't do that now on the server side (easily).

    Posted: 9 months ago #
  4. Ipstenu (Mika Epstein)
    Administrator

    Ah...

    Okay you're right, you're doing it wrong.

    The RIGHT way to gather those statistics is Javascript.

    Posted: 9 months ago #
  5. razvanm15
    Member

    We seem to be getting into arguments on what is rigth / wrong / correct writing code. I don't want to do that.

    I just had a suggestion, feedback on that will be great, if any.

    Thank you.

    Posted: 9 months ago #
  6. Ipstenu (Mika Epstein)
    Administrator

    Okay, the feedback is that you're using code outside of it's intended and supported purpose. If you want this to work properly, you should use it properly.

    Which I get it, it's a really horrible answer, but this statement is not correct:

    I understand that doing a POST might not be the best method or the 'correct' method, it does not matter, a complete framework should allow you to do what you need.

    Or rather, it is correct, but it DOES allow you do to what you need. What you need is to gather stats on searches, passing them to Google etc. Cool. It does that, but you must use javascript, because you're not using the default search form function.

    You could also filter the default search form

    https://highedwebtech.com/2013/11/24/wordpress-google-analytics/
    https://developer.wordpress.org/reference/functions/get_search_form/

    Because at the end of the day, a proper CMS does let you do anything, but it also kinda requires you use the CMS to do it :) Which you're not.

    Posted: 9 months ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    1 Vote
  • Status

    This idea is under consideration