在自定义仪表板上显示打开的备注数量

时间:2013-03-16 作者:Mark

在自定义仪表板上,我想show the number of total new comments (某种程度上就像“现在”一样)。就像“一样”;你有。。。打开要处理的注释;。所以我想要的只是总数,而不是每个帖子的列表。

但我做不好。有什么帮助吗?

与此同时,我(从wpmudev那里)得到了一些关于这方面的信息,告诉我这是一个沉重的问题,不是一个非常基本的问题:

这没有你想象的那么简单。

您需要:

向WP管理员注册一个新的元框,查询数据库中自当前登录用户上次访问以来的所有新评论,设置访问超时(因此更改页面不仅仅是立即将其设置为0,这将很令人沮丧)

  • 编写CSS以使显示美观
  • 如果您在"E;“现在”;但这可能会超出你的想象

    再次感谢@toscho,这才是我最终感到高兴的地方。它显示了等待审核的评论数量。它可以放在任何你想让它出现的地方。

    <?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.";
    ?>
    

  • 1 个回复
    最合适的回答,由SO网友:fuxia 整理而成

    Actually, it is not that hard.

    • The last access time for a user is in get_user_meta( get_current_user_id(), \'last_access\', TRUE ).
    • The date of each comment is in the column comment_date.
    • Both share the same format, so we can compare them in SQL with a simple >.
    • There is an action in the Right Now dashboard widget to show additional rows: right_now_discussion_table_end. See the file wp-admin/includes/dashboard.php.

    Now let’s stick it together:

    <?php  # -*- coding: utf-8 -*-
    /**
     * Plugin Name: T5 New Comments In Right Now Dashboard
     * Description: Show the number of new comments on the Right Now dashboard
     * Plugin URI:
     * Version:     2013.03.16
     * Author:      Thomas Scholz
     * Author URI:  http://toscho.de
     * Licence:     MIT
     * License URI: http://opensource.org/licenses/MIT
     */
    
    add_action( \'right_now_discussion_table_end\', \'t5_new_comments_right_now\' );
    
    function t5_new_comments_right_now()
    {
        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", $last_access );
        $comment_query = $wpdb->get_results(
            "SELECT comment_ID,
                COUNT( comment_ID ) AS new_comments
                FROM {$wpdb->comments} $where",
                OBJECT
        );
    
        // default values
        $num  = 0;
        $text = _x(
            \'New comments\',
            \'no new comments on dashboard right now\',
            \'plugin_t5_new_comments\'
            );
    
        // overwrite default values
        if ( isset ( $comment_query[0]->new_comments ) ) {
            $num = $comment_query[0]->new_comments;
            $text = _n( \'New comment\', \'New comments\', $num, \'plugin_t5_new_comments\' );
        }
    
        // prepare for display
        $num  = number_format_i18n( $num );
        $num  = "<a href=\'edit-comments.php\'><span class=\'total-count\'>$num</span></a>";
        $text = "<a href=\'edit-comments.php\'>$text</a>";
    
        // display extra column
        printf(
            \'<tr>
                <td class="b b-comments">%1$s</td>
                <td class="last t comments">%2$s</td>
            </tr>\',
            $num,
            $text
        );
    }
    

    Result:

    screenshot

    Download from GitHub


    In response to your comments: To get just the number of new comments a an integer, use something like this:

    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", $last_access );
        // to get unapproved comments only use this instead:
        // $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;
    }
    

    Now you can use that function in your custom code:

    $new_comments = t5_count_new_comments();
    echo "There are $new_comments new comments.";
    
    结束

    相关推荐

    Reorder dashboard widgets

    我最近试图通过编写插件对仪表板小部件进行重新排序。我做到了这一点:使用以下代码:<?php /* * Plugin Name: Custom Dashboard * Description: Custom dashboard for Avare sites. * Author: Avare * Version: 1.0 */ function sort_dashboard_widgets() { $lef