Wordpress, add custom button on post type list page

我只是一个虾纸丫 提交于 2020-07-08 07:01:15

问题


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


回答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:


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


来源:https://stackoverflow.com/questions/29811433/wordpress-add-custom-button-on-post-type-list-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!