Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a multi author site and I have already used some hooks to stop them from viewing other author's posts and media. Now, in the Wordpress Dashboard in the "at a glance" section it shows them the total posts and comments. They can click on them at the moment and I would like them to not be able to do this.

So, I have found a hook which targets this, although I'm only starting to learn how to customise using hooks so I need some help. Here is what I have right now, just echoing out the current user's ID to verify that it is the right hook:

add_filter('dashboard_glance_items', 'make_unclickable');

function make_unclickable( $items = array() ) {
    global $current_user;
    echo $current_user->ID;

    return $items;
}

So, how can I make the links unclickable?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer

Unfortunately, there is no 'filter' to remove or edit the previous items in the 'At Glance' box.

EDIT

I removed the "Wordpress Core Hack" part because I don't want to encourage such a behavior, and I don't want you to edit the code every time Wordpress releases a new version :)

And here is a code to append custom post types stats without being clickable :

add_filter('dashboard_glance_items', 'make_unclickable');

function make_unclickable( $items  ) {

 $post_types = array( 'post_type_1', 'post_type_2' );

foreach( $post_types as $type ) {

    if( ! post_type_exists( $type ) ) continue;

    $num_posts = wp_count_posts( $type );

    if( $num_posts ) {

        $published = intval( $num_posts->publish );
        $post_type = get_post_type_object( $type );

        $text = _n( '%s ' . $post_type->labels->singular_name, '%s ' . $post_type->labels->name, $published, 'your_textdomain' );
        $text = sprintf( $text, number_format_i18n( $published ) );



            $items[] = sprintf( '<span class="%1$s-count">%2$s</span>', $type, $text ) . "\n";

    }
}

return $items;
 }
share|improve this answer
    
I think I found a way around your above WordPress Core hack. I can't upvote your answer, while you have the core hack part in your answer ;-) –  birgire Aug 24 at 16:03
    
Even if I wrote "-I don't prefer that-" ? :-) –  Waseem Abu Senjer Aug 24 at 16:57
    
Instead explain the source code part and remove phrases like "If you insist doing that, you can hack the Wordpress core manually" ... well that's my suggestion because I'm afraid some people will not take your warning seriously enough ;-) –  birgire Aug 24 at 17:06
1  
You're absolutely right, I edited my answer :) –  Waseem Abu Senjer Aug 24 at 17:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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