Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create links like the ones in the post tab. See Image Below. enter image description here

I created my WP_List_table but I am not sure how to create those links or how to get them to show a different db query.

I know about the extra_tablenav function in the class but this adds things after the bulk action area, so this doesn't seem to do what I want.

share|improve this question
    
    
Please see my solution to your question in this post. Close voting this a duplicate of that question. –  Stephen Harris Jun 29 '12 at 12:22
    
Ignore that - this is a custom instance of WP_List_Table. Will post solution below. –  Stephen Harris Jun 29 '12 at 12:24

2 Answers 2

As mentioned in the comments, if this is an existing table you wish to add/remove links from, see this answer.

If this a custom subclass of WP_List_Table, e.g.:

class Wpse56883_WP_List_Table extends WP_List_Table{

 //Class methods here

}

Then you add these links by over-riding the get_views() method. This should return an array: array ( id => link ). This should link back to the page, attaching some value to a query variable (which I'll call customvar).

When constructing the links, check for the current value for customvar add conditionally add the class current to highlight in bold the current view.

So (inside) your class.

function get_views(){
   $views = array();
   $current = ( !empty($_REQUEST['customvar']) ? $_REQUEST['customvar'] : 'all');

   //All link
   $class = ($current == 'all' ? ' class="current"' :'');
   $all_url = remove_query_arg('customvar');
   $views['all'] = "<a href='{$all_url }' {$class} >All</a>"

   //Foo link
   $foo_url = add_query_arg('customvar','foo');
   $class = ($current == 'foo' ? ' class="current"' :'');
   $views['foo'] = "<a href='{$foo_url}' {$class} >Foo</a>"

   //Bar link
   $bar_url = add_query_arg('customvar','bar');
   $class = ($current == 'bar' ? ' class="current"' :'');
   $views['bar'] = "<a href='{$bar_url}' {$class} >Bar</a>"

   return $views;
}

Then in your prepare_items method you can retrieve the customvar method and alter your query according to it's value.

 function prepare_items(){
     //Retrieve $customvar for use in query to get items.
     $customvar = ( isset($_REQUEST['customvar']) ? $_REQUEST['customvar'] : 'all');
 }

Note: the links can be used to perform actions. I would store the 'action' value in the query variable action (remember to use nonces!). Then hook onto load-{$hook} (see Codex), check permissions and nonces, and then perform the action.

If you are going to include 'action links', make sure you use nonces - and you should only display the link for users with the necessary capabilities.

share|improve this answer
    
Hi, I am creating a custom class that extends WP_List_Table. And I am trying to use the get_views() method to add links on top of my table. But it is never called. Is there anything else that have to be done so that method links appears? –  nacho3d Feb 25 at 0:22
    
Yes! You have to call it manually. Your code will look like: $table = new Table(); $table->prepare_items(); $table->views(); $table->display(); –  yves amsellem Aug 29 at 17:11

You have to call views(); when rendering the page content.

Put <?php $wp_list_table->views(); ?> after page title and before the form.

See: \wp-admin\edit.php to see when is called.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.