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 am using WordPress comments on a page as a Contact form. Comments from my Contact form page are set to auto approve and only visible to the logged admin. Now in the dashboard I would like to separate my Contact form comments from the other comments. I would like to do it either by placing them in a theme option page or by using a filter on the comments page. Why I wanna do this new way? Because I believe it will be more user friendly.

share|improve this question
add comment

1 Answer

Based on this Answer, it was easy.

Result:
comments from a single post

Code:
check the comments

// ID of the post/page that we want a separate list of comments
$the_id = '1';

add_action( 'current_screen', 'wpse_73581_exclude_lazy_hook' , 10, 2 );

/**
  * Delay hooking our clauses filter to ensure it's only applied when needed. 
  */
function wpse_73581_exclude_lazy_hook( $screen )
{
    if ( $screen->id != 'edit-comments' )
        return;

    // Check if our Query Var is defined    
    if ( isset( $_GET['hello_world'] ) )
        add_action( 'pre_get_comments', 'wpse_73581_list_one_post_comments', 10, 1 );

    add_filter( 'comment_status_links',  'wpse_73581_link_to_hello_world' );
}


/**
 * Only display comments from specific post
 */
function wpse_73581_list_one_post_comments( $clauses )
{
    global $the_id;
    $clauses->query_vars['post_id'] = $the_id;
}

/**
 * Add link to list of comments from specific post with counter
 */
function wpse_73581_link_to_hello_world( $status_links )
{
    global $wpdb, $the_id;

    // Get the number of comments from our post
    $count = count(
            $wpdb->get_results(
                    $wpdb->prepare(
                            "SELECT comment_ID FROM $wpdb->comments 
                            WHERE comment_post_ID = $the_id
                            AND comment_approved != 'trash'", 
                            ARRAY_A
                    )
            )
    );

    // Showing our special page
    if ( isset( $_GET['hello_world'] ) )
    {
        // So we can remove the "current" class that is applied to this link
        $status_links['all'] =
                '<a href="edit-comments.php?comment_status=all">'
                . __( 'All' )
                . '</a>';

        // Our link with "current" class and comments count
        $status_links['hello_world'] =
                '<a href="edit-comments.php?comment_status=all&hello_world=1" class="current" style="margin-leff:60px">'
                . __( 'Hello World' )
                . ' <span class="count">('
                . $count
                . ')</a></span>';
    }
    // Showing other comments pages (All, Pending, Approved, etc)
    else
    {
        $status_links['hello_world'] =
                '<a href="edit-comments.php?comment_status=all&hello_world=1" style="margin-leff:60px">'
                . __( 'Hello World' )
                . '  <span class="count">('
                . $count
                . ')</span></a>';
    }

    return $status_links;
}
share|improve this answer
add comment

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.