count comments on custom dashboard

On my custom dashboard, I want to show the number of total new comments. Just like "You have ... new comments to take care of"

Can't get this right, any help out there?

  • Imperative Ideas

    This is less basic than you might think.

    You need to:

    a) Register a new meta box to the WP admin
    b) Query the database for the total new comments since the currently logged in user's last visit
    c) Set the visit timeout (so changing page doesn't just set them to 0 right away, that would be frustrating)
    d) Write the CSS to make the display pretty
    e) You'll find hints as to what to do if you examine the WordPress core in the "Right Now" widget but that's probably going to be over your head.
    At that point, you may as well publish it as a plugin. It's possible and as far as plugins go, it's barely intermediate, but it's still very advanced for this forum.

    You'll have better luck with this sort of question here: http://wordpress.stackexchange.com/

    If it's simple enough, someone may even dash of the code for it. That's a stretch though... forums are a very "help me help you" type environment.

  • Mark

    Hi Ari, thank you but not at the moment. @Imperative was quite overwhealming and I am trying stackexchange now. Let's see what I get. To put it together, by myself is a little too much for me at this stage. I just thought it might be simple to do. It is exactly what is showing up behind the menu "comments" tab in the admin menu, if you have open comments. It is just that number in that circle that I want to include on the custom dashboard. It is not huge importand to have it but it would look nice and since the code must already exsist (otherwise the number would not show up behind the comment menu tab), I thought it might be easy to grab it from somewhere and paste it into my dashboard page. Obviously I was wrong with that.

    Thanks for taking care of me.

    :wink:

  • Mark

    Hi there, it is done now. This is what it needed:

    <?php
    function t5_count_new_comments()
    {
        global $wpdb;
    
        // last user access
        $last_access   = get_user_meta( get_current_user_id(), 'last_access', TRUE );
    
        // comment query
    	$where = $wpdb->prepare( "WHERE comment_date > %s AND comment_approved='0'", $last_access );
        $comment_query = $wpdb->get_results(
            "SELECT comment_ID,
            COUNT( comment_ID ) AS new_comments
            FROM {$wpdb->comments} $where",
            OBJECT
            );
    
        if ( ! isset ( $comment_query[0]->new_comments ) )
            return 0;
    
        return $comment_query[0]->new_comments;
    
    }
    
    $new_comments = t5_count_new_comments();
    echo "There are $new_comments new comments.";
    ?>
  • Mark

    ....but maybe there is another issue you guys can help me with.

    The container, that is showing the number of the comments, is showing a black background on default. I would like to change it to red, if the number is higher than 0. Got the idea? Is this a jQuery/php combination? How can I imagine to get this done?

    It is quite a burden to be creative but not to have enough skills to make it happen :slight_frown:

  • aristath

    Hello there @Mark, I hope you're well today!
    At the end of your function you do
    echo "There are $new_comments new comments.";
    To change the background to red, you'd do something like this:

    if ( $new_comments > 0 ) {
      echo '<span style="background: #cc0000;">;
      echo 'There are $new_comments new comments.';
      echo '</span>';
    } else {
      echo 'There are $new_comments new comments.';
    }

    I hope that helps!

    I am marking this issue as resolved.
    If however the above fix doesn't work for you or you need any further instructions/clarifications, don't hesitate to reopen this and post an update on your current status. :slight_smile:

    Cheers,
    Ari.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.