admin管理员组文章数量:1134557
I try to get rid of the comments list in the WordPress 4 dashboard section but it doesn't work.
Here is what I tried:
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');
The Quick press and Primary boxes are unset, so it works but the recent comments remain on the screen.
The example above were tested at the same time. I tried one by one, but it doesn't work
I also tried to hide the section with CSS by adding:
#latest_comments {display:none !important;}
But it still doesn't work.
Any idea?
I try to get rid of the comments list in the WordPress 4 dashboard section but it doesn't work.
Here is what I tried:
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');
The Quick press and Primary boxes are unset, so it works but the recent comments remain on the screen.
The example above were tested at the same time. I tried one by one, but it doesn't work
I also tried to hide the section with CSS by adding:
#latest_comments {display:none !important;}
But it still doesn't work.
Any idea?
Share Improve this question edited Jan 5, 2016 at 15:31 birgire 67.8k7 gold badges119 silver badges251 bronze badges asked Jan 5, 2016 at 13:42 BaylockBaylock 3851 gold badge4 silver badges15 bronze badges1 Answer
Reset to default 2The recent comments list is part of the Activity Dashboard Widget.
Approach #1
We could remove that dashboard widget and then add our modified version of it:
/**
* 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'
);
} );
where our custom callback (without comments) is:
/**
* 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>';
}
We might have used instead:
// No comments!
$recent_comments = wp_dashboard_recent_comments( $total_items = 0 );
Approach #2
Another shorter approach would be to hook into the dashboard_recent_posts_query_args
filter on the dashboard page and remove the comments:
/**
* 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;
} );
} );
Approach #3
We could then add further restrictions, like only empty the comments once:
/**
* 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;
} );
} );
Approach #4
If we want to reduce the comments database query, then we could use the pre_get_comments
hook instead.
Here's one such idea:
/**
* 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;
} );
} );
where we use the count
query variable of WP_Comment_Query
to minimize the database query.
Approach #5
Here's even a more drastic approach:
/**
* 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;
} );
} );
where we just eliminate that comment query.
Approach #6
Finally here's the CSS version:
/**
* 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>';
} );
本文标签: AdminDashboardUnset recent comments
版权声明:本文标题:Admin - Dashboard - Unset recent comments 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736860815a1955906.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论