管理-仪表板-取消设置最近的评论

时间:2016-01-05 作者:Baylock

我试图去掉WordPress 4仪表板部分的评论列表,但它不起作用。

以下是我尝试的内容:

    function remove_all_dashboard_widgets() 
    {
        global $wp_meta_boxes;

        // these 2 are ok as they are not displayed anymore
        unset($wp_meta_boxes[\'dashboard\'][\'side\'][\'core\'][\'dashboard_quick_press\']);
        unset($wp_meta_boxes[\'dashboard\'][\'side\'][\'core\'][\'dashboard_primary\']);

        // I tried both "recent" and "latest comments" with no success
        unset($wp_meta_boxes[\'dashboard\'][\'normal\'][\'core\'][\'dashboard_recent_comments\']);
        unset($wp_meta_boxes[\'dashboard\'][\'normal\'][\'core\'][\'dashboard_latest_comments\']);

        // I also tried these two with no success
        remove_meta_box(\'dashboard_recent_comments\', \'dashboard\', \'core\');
        remove_meta_box(\'dashboard_latest_comments\', \'dashboard\', \'core\');
    }
    add_action(\'wp_dashboard_setup\', \'remove_all_dashboard_widgets\');
快速按下和主框未设置,因此它可以工作,但最近的评论仍保留在屏幕上。

同时对上述示例进行了测试。我一个接一个地试了,但没用

我还试图通过添加以下内容来隐藏CSS部分:

#latest_comments {display:none !important;}
但它仍然不起作用。

有什么想法吗?

1 个回复
SO网友:birgire

最近的评论列表是活动仪表板小部件的一部分。

方法#1

我们可以删除该仪表板小部件,然后添加它的修改版本:

/**
 * Remove the latest comments from the Activity dashboard widget (approach #1)
 */
add_action( \'wp_dashboard_setup\', function()
{
    // Remove the Activity widget
    remove_meta_box( \'dashboard_activity\', \'dashboard\', \'normal\' );

    // Add the modified Activity widget
    if ( is_blog_admin() ) 
       wp_add_dashboard_widget( 
           \'wpse_dashboard_activity\', 
           __( \'Activity\' ), 
           \'wpse_dashboard_site_activity\' 
       );

} );
其中,我们的自定义回调(无注释)是:

/**
 * Custom Activity Widget callback
 *
 * @see wp_dashboard_site_activity()
 */
function wpse_dashboard_site_activity() {

        echo \'<div id="activity-widget">\';

        $future_posts = wp_dashboard_recent_posts( array(
                \'max\'     => 5,
                \'status\'  => \'future\',
                \'order\'   => \'ASC\',
                \'title\'   => __( \'Publishing Soon\' ),
                \'id\'      => \'future-posts\',
        ) );
        $recent_posts = wp_dashboard_recent_posts( array(
                \'max\'     => 5,
                \'status\'  => \'publish\',
                \'order\'   => \'DESC\',
                \'title\'   => __( \'Recently Published\' ),
                \'id\'      => \'published-posts\',
        ) );

        // No comments!    
        $recent_comments = false; // wp_dashboard_recent_comments();

        if ( !$future_posts && !$recent_posts && !$recent_comments ) {
                echo \'<div class="no-activity">\';
                echo \'<p class="smiley"></p>\';
                echo \'<p>\' . __( \'No activity yet!\' ) . \'</p>\';
                echo \'</div>\';
        }

        echo \'</div>\';
}
我们可能会使用:

// No comments!    
$recent_comments = wp_dashboard_recent_comments( $total_items = 0 );
另一种较短的方法是dashboard_recent_posts_query_args 在仪表板页面上筛选并删除注释:

/**
 * Remove the latest comments from the Activity Dashboard widget (approach #2)
 */
add_action( \'load-index.php\', function(){
    add_filter( \'dashboard_recent_posts_query_args\', function( $args )
    {
        add_filter( \'the_comments\', \'__return_false\' );
        return $args;
    } );    
} );
然后,我们可以添加更多限制,例如只清空一次注释:

/**
 * Remove the latest comments from the Activity Dashboard widget (approach #3)
 */
add_action( \'load-index.php\', function(){
    add_filter( \'dashboard_recent_posts_query_args\', function( $args )
    {
        add_filter( \'the_comments\', function( $comments )
        {
            static $instance = 0;
            $comments = ( 1 === ++$instance ) ? false : $comments;
            return $comments;
        } );
        return $args;
    } );    
} );
如果我们想减少注释数据库查询,那么我们可以使用pre_get_comments 改为挂钩。

这里有一个这样的想法:

/**
 * Remove the latest comments from the Activity Dashboard widget (approach #4)
 */
add_action( \'load-index.php\', function()
{
    add_filter( \'dashboard_recent_posts_query_args\', function( $args )
    {
        // Let\'s exit the WP_Comment_Query from the get_comments_ids() method:
        add_action( \'pre_get_comments\', function( \\WP_Comment_Query $q )
        {
            if( 1 === did_action( \'pre_get_comments\' ) )
                $q->set( \'count\', true );

            // Override the WHERE part
            add_filter( \'comments_clauses\', function( $clauses )
            {
                if( 1 === did_action( \'pre_get_comments\' ) )
                    $clauses[\'where\'] = \' 0=1 \';
                return $clauses;
            }, PHP_INT_MAX );

        } );
        return $args;
    } );    
} );
我们使用count 的查询变量WP_Comment_Query 最小化数据库查询。

方法#5

这里有一个更激进的方法:

/**
 * Remove latest comments from the Activity Dashboard widget (approach #5)
 */
add_action( \'load-index.php\', function()
{
    add_filter( \'dashboard_recent_posts_query_args\', function( $args )
    {
        // Let\'s exit the WP_Comment_Query from the get_comments_ids() method:
        add_action( \'pre_get_comments\', function( \\WP_Comment_Query $q )
        {
            if( 1 === did_action( \'pre_get_comments\' ) )
                $q->set( \'count\', true );

            // Eliminate the next query         
            add_filter( \'query\', function( $query )
            {
                static $instance = 0;
                if( 1 === ++$instance )
                    $query = false;
                return $query;
            }, PHP_INT_MAX );

        } );
        return $args;
    } );    
} );
我们只是消除了注释查询。

方法#6

最后是CSS版本:

/**
 * Hide the latest comments from the Activity Dashboard widget (approach #6)
 */
add_action( \'admin_head-index.php\', function()
{
    print \'<style>#latest-comments{ display:none; }</style>\';
} );

相关推荐

在将代码添加到函数后无法登录WordPress wp-admin。php

我在函数末尾添加以下代码。php文件,用于根据自定义帖子的帖子标题填充分类法。问题是,当我添加代码时,尝试登录wp admin时会出现以下错误。非常感谢您能帮助我们弄清楚为什么会发生这种情况。Error:错误:由于意外输出,Cookie被阻止。有关帮助,请参阅此文档或尝试支持论坛。Code: <?php function update_custom_terms($post_id) { // only update terms if