WordPress.org

Forums

Custom List Table Example
Add custom filter (5 posts)

  1. shinji251184
    Member
    Posted 3 years ago #

    This plugin is perfect to create new admin screens ;)
    There's a example of how to add a "builk action" (delete...) but I need to add a custom filter and I had some problem because I don't know how to put the code.

    Here is what I wrote. :

    function get_bulk_actions() {
      $actions = array(
      // I don't need to delete post in my case
      //'delete'    => 'Delete'
      );
    
      // Display date
      // For this example, I use a fonction in class-wp-liste-table.php
      // that display months (I use my own function to display months but this one works for the example)
      $this->months_dropdown('post');
      submit_button( __( 'Filter' ), 'secondary', false, false, array( 'id' => 'post-query-submit' ) );
    
      return $actions;
    }

    This code displays a drop-down list with months with a submit button.
    But when I submit, I have some url parameters (get) that shouldn't have to be there.

    admin.php?page=tt_list_test&_wpnonce=f008999aef&_wp_http_referer=%2Fwp-admin%2Fadmin.php%3Fpage%3Dtt_list_test

    _wpnonce & _wp_http_referer shouldn't have to be url parameters.

    And if I sort a column and change the month, it loses the order

    Order :
    /admin.php?page=tt_list_test&orderby=nom&order=desc
    =>
    Change date :
    /admin.php?page=tt_list_test&_wpnonce=f008999aef&_wp_http_referer=%2Fwp-admin%2Fadmin.php%3Fpage%3Dtt_list_test%26orderby%3Dnom%26order%3Ddesc&m=201111&paged=1

    Thank for your help.

    http://wordpress.org/extend/plugins/custom-list-table-example/

  2. David Gard
    Member
    Posted 3 years ago #

    I think that you need to add $this->months_dropdown() in the extra_tablenav() function.

  3. shinji251184
    Member
    Posted 3 years ago #

    I tried what you say, but the problem was that in extra_tablenav, input are generated twice (and so the $_GET parameters, maybe I did it wrong) and I use a custom function to generate month (every months, not only months when posts are published).

    Here's a solution (I'm not sure if the code is in the right place) :

    function get_bulk_actions() {
        // function to generate month
        $this->month_select();
        return $actions;
    }

    So my <select> appears inside the <form> in the right place.

    And I wrote this code at the beginning of the plugin, under require(ABSPATH . '/wp-load.php');, to avoid url parameters trouble :

    require(ABSPATH . 'wp-includes/pluggable.php');
    
    if ( ! empty($_REQUEST['_wp_http_referer']) && $_REQUEST['page']=='tt_list_test') {
    $url_page_admin = stripslashes($_SERVER['REQUEST_URI']);
    wp_redirect( remove_query_arg( array('_wp_http_referer', '_wpnonce'), stripslashes($_SERVER['REQUEST_URI']) ) );
    exit;
    }

    And finaly, I add some input type="hidden" and put order and orderby value, that disapear with redirection :

    if ( ! empty( $_REQUEST['orderby'] ) )
                    echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
                if ( ! empty( $_REQUEST['order'] ) )
                    echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
  4. Alex-A
    Member
    Posted 3 years ago #

    Hi There,

    I just had a 'WTF is going on session' for a couple of hours dealing with the same issue. I also had the same problem that my custom filter was generated twice when i used it.

    But! i've found the solution!! So i thought i would share it.

    First thing is that you DO need to use extra_tablenav() for adding extra custom filters.

    Now the reason WHY in that place the input is called twice, is because the extra_tablenav() generates the input twice ( at the top of the table and bottom ), so if you only provide one form or select field, the same parameter get called twice ( top / bottom ).

    And when putting out a request every input is collected and put out.

    To deal with that you have to provide a different input name for top and bottom, OR only put your filter at the top.

    Do this so, by using the extra_tablenav() on the following way:

    function extra_tablenav($which) {
    	if ( $which == "top" ){
    // GENERATE FILTER FOR TOP
    }
    	if ( $which == "bottom" ){
    // FILTER FOR BOTTOM
    }
    }

    But like i said, you also could only use one of them ( top bottom )

    Hope it helps someone!

  5. designerken
    Member
    Posted 2 years ago #

    @shinji251184
    I too have the issue with the _wpnonce and the _wp_http_referer appearing in the URL. The $_SERVER['REQUEST_URI'] doubles on itself and eventually I get the error that the URL is too long.

    I am not too clear on where you put the code:

    require(ABSPATH . 'wp-includes/pluggable.php');
    
    if ( ! empty($_REQUEST['_wp_http_referer']) && $_REQUEST['page']=='tt_list_test') {
    $url_page_admin = stripslashes($_SERVER['REQUEST_URI']);
    wp_redirect( remove_query_arg( array('_wp_http_referer', '_wpnonce'), stripslashes($_SERVER['REQUEST_URI']) ) );
    exit;
    }

    Could you make a pastebin?

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Custom List Table Example
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic