0

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?

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.