最近的评论列表是活动仪表板小部件的一部分。
方法#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>\';
} );