6

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.

11

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 overriding 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 and 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.

| 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? – iVela Feb 25 '14 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 '14 at 17:11
2

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.

| improve this answer | |
0

For this problem i have created a custom style , you can follow

//Add a filter hook first 

add_filter('views_edit-your_custom_post_name',array($this,'prefix_post_status_links'));

 function prefix_post_status_links($views) {
         ?>
        <div class="gwf-tab-header">
            <ul class="nav nav-pills">
                <?php
                $views = array(
                 'all' => '
                <li class="nav-item">
                    <a class="nav-link active p-0" href="edit.php?post_type=your_custom_post_name">
                        <i class="fas fa-circle"></i>
                        All Entries
                    </a>
                </li>',
                'active' => '<li class="nav-item">
                    <a class="nav-link p-0 text-success" href="edit.php?post_status=active&post_type=your_custom_post_name">
                        <i class="far fa-circle"></i>
                        Active
                    </a>
                </li>',
                'inactive' => '<li class="nav-item">
                    <a class="nav-link p-0 text-warning" href="edit.php?post_status=inactive&post_type=your_custom_post_name">
                        <i class="far fa-circle"></i>
                        Inactive
                    </a>
                </li>',
                'trash' => '<li class="nav-item">
                    <a class="nav-link p-0 text-danger" href="edit.php?post_status=trash&post_type=your_custom_post_name">
                        <i class="far fa-circle"></i>
                        Trash
                    </a>
                </li>'
                );
                 return $views;
                ?>
            </ul>
        </div>
        <?php
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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