Home » Php » php – WordPress, add custom button on post type list page

php – WordPress, add custom button on post type list page

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

Thanks

How to&Answers:

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
}

Answer:

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.

Answer:

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>');