WordPress, add custom button on post type list page

I am trying to add custom button on top of post type page like this image
enter image description here

Is there any filter or action I can use to add custom button there?

Read More

Thanks

Related posts

Leave a Reply

7 comments

  1. I found a way to get it done but I am not very happy with this procedure. Please add your answer if you find better way. Mean while, this might be of help.

    add_action('admin_head-edit.php','addCustomImportButton');
    

    I only need this on edit page, so I am using admin_head-edit.php action, but you can use admin_head or some other (not very specific requirement)

    /**
     * Adds "Import" button on module list page
     */
    public function addCustomImportButton()
    {
        global $current_screen;
    
        // Not our post type, exit earlier
        // You can remove this if condition if you don't have any specific post type to restrict to. 
        if ('module' != $current_screen->post_type) {
            return;
        }
    
        ?>
            <script type="text/javascript">
                jQuery(document).ready( function($)
                {
                    jQuery(jQuery(".wrap h2")[0]).append("<a  id='doc_popup' class='add-new-h2'>Import</a>");
                });
            </script>
        <?php
    }
    
  2. Digging into WordPress core code i did not find any hook or any filter for that buttonm you can also see that code from line no 281 to line number 288 . But you can add your button here according to this filter.

    add_filter('views_edit-post','my_filter');
    add_filter('views_edit-page','my_filter');
    
    function my_filter($views){
        $views['import'] = '<a href="#" class="primary">Import</a>';
        return $views;
    }
    

    Hope it helps you.

  3. If you are using the class WP_Lists_table (and you should) then this is the right way to do it:

    add_action('manage_posts_extra_tablenav', 'add_extra_button');
    function add_extra_button($where)
    {
        global $post_type_object;
        if ($post_type_object->name === 'shop_order') {
            // Do something
        }
    }
    
  4. The accepted answer is sadly still the only one that works.

    But as the admin heading changed since it was answered, the correct script should now be :

    jQuery(jQuery(".wrap .page-title-action")[0]).after('<a href="#" class="page-title-action">Import</a>');
    
  5. Unfortunately there is no hook called after showing the “Add New” button.
    The closest possible place to add anything without using javascript is below the title and “Add new” like this:

    enter image description here

    In my example I added a button to my custom post type “Event” using the hook “edit_form_top”:

    add_action('edit_form_top', 'add_custom_button');
    
    function add_custom_button($id){
        if ($post->post_type != 'event') return false;
        echo('<button>Custom button</button>');
    }
    
  6. The answer of Touqeer Shafi got me in the right direction, I needed to add a button at the top of my table view for a custom post type (book), I only had to change the post part in the views_edit-post to make it work:

    add_action('views_edit-book', function($id) {
          echo('<a href="/post-new.php?post_type=book" class="page-title-action">Another new book</a>');
        });
    
  7. Now in 2022 with WP 5.9.1, I combined answers from Geza Gog and Tameroski, and it works great:

    add_action('edit_form_top', 'my_import_button');
    function my_import_button(){
        ?>
        <script type="text/javascript">
            jQuery(document).ready( function($)
            {
                jQuery(jQuery(".wrap .page-title-action")[0]).after('<a href="#" class="page-title-action">Import</a>');
            });
        </script>
        <?php
    }