How to make post and comment count unclickable with dashboard_glance_items hook

The dashboard_glance_items filter is only useful for modifying the extra elements. The posts/comments data elements have already been displayed.

Here are three ideas:

Method #1 – Use the dashboard_glance_items filter:

You can use the following filter setup, to remove the posts/pages/comments elements from the output of wp_dashboard_right_now().

The trick is simple, foul WordPress to think there are no posts/comments/pages.

Here’s one implementation (I’m sure I can refine this further):

add_action( 'do_meta_boxes', 'custom_do_meta_boxes', 99, 2 );

function custom_do_meta_boxes( $screen, $place )
{
    if( 'dashboard' === $screen && 'normal' === $place )
    {   
        add_filter( 'wp_count_posts', 'custom_wp_count_posts' );
        add_filter( 'wp_count_comments', 'custom_wp_count_comments' );
    }
}

function custom_wp_count_posts( $stats )
{
    static $filter_posts = 0;
    if( 1 === $filter_posts )
        remove_filter( current_filter(), __FUNCTION__ );

    $filter_posts++;
    return null;
}

function custom_wp_count_comments( $stats )
{
    static $filter_comments = 0;
    if( 1 === $filter_comments )
        remove_filter( current_filter(), __FUNCTION__ );

    $filter_comments++;
    return array( 'total_comments' => 0 );
}

Then you can add the posts/pages/comments elements again via the dashboard_glance_items filter.

Method #2 – Reuse the wp_dashboard_right_now() output:

Here’s one hack, where we remove the current Right Now widget and add another Custom Right Now widget:

/**
 * Replace the 'Right Now' dashboard widget with our own.
 */

add_action('wp_dashboard_setup',                               
    function()
    {
        // Remove the current 'Right Now' dashboard widget:
        remove_meta_box(
           'dashboard_right_now', 
           'dashboard', 
           'normal'  
        );

        // Add our 'Custom Right Now' dashboard widget:
        add_meta_box(
           'custom_wp_dashboard_right_now',
           __( 'Custom Right Now' ),
           'custom_wp_dashboard_right_now', 
           'dashboard', 
           'normal', 
        );

    }
);   

where our simple demo callback is:

function custom_wp_dashboard_right_now()
{
        // Let wp_dashboard_right_now() do all the hard work:
        ob_start();
        wp_dashboard_right_now();
        $html = ob_get_contents();                                  
        ob_end_clean();

        // Modify the output.

        // Simple example where all links are stripped out:
        echo strip_tags( $html, '<p><div><span><ul><ol><li>' );
}

Here we use the output buffering to catch the content from wp_dashboard_right_now() and then replace all the links from it.

This is just a simple example. You might need to use preg_replace() to target only the posts/comments items.

You could also pick up the relevant parts from the wp_dashboard_right_now() core function to use in the custom_wp_dashboard_right_now() callback.

Method #3 – CSS/Javascript

We could also modify these links via CSS/Javascript. But I leave the implementation to you 😉


I hope you can modify this to your needs.

Leave a Comment