Wordpress,在帖子类型列表页面上添加自定义按钮

use*_*820 6 php wordpress wordpress-plugin

我试图在帖子类型页面顶部添加自定义按钮,就像这张图片一样 在此输入图像描述

我可以使用任何过滤器或操作在那里添加自定义按钮吗?

谢谢

use*_*820 11

我找到了一种方法来完成它,但我对这个程序不是很满意.如果你找到更好的方法,请添加你的答案.同时,这可能会有所帮助.

add_action('admin_head-edit.php','addCustomImportButton');
Run Code Online (Sandbox Code Playgroud)

我只在编辑页面上需要这个,所以我正在使用admin_head-edit.php动作,但你可以使用admin_head或其他一些(非常具体的要求)

/**
 * 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
}
Run Code Online (Sandbox Code Playgroud)

  • 由于 HTML 更改,不再起作用。(jQuery将元素添加到不可见区域) (2认同)
  • 由于仍然没有更好的替代方案,因此 JS 应更改为: `&lt;script type="text/javascript"&gt; jQuery(function () { jQuery('hr.wp-header-end').before("&lt;a id='doc_popup' class='add-new-h2'&gt;导入&lt;/a&gt;"); }); &lt;/脚本&gt;` (2认同)

Mac*_*408 10

如果您正在使用 WP_Lists_table 类(并且您应该),那么这是正确的方法:

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
    }
}
Run Code Online (Sandbox Code Playgroud)


Tou*_*afi 5

深入研究WordPress核心代码,我没有找到该buttonm的任何钩子或任何过滤器,您还可以看到从281行到288行的代码。但是您可以根据此过滤器在此处添加按钮。

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;
}
Run Code Online (Sandbox Code Playgroud)

希望对您有帮助。