Is it possible to show custom comment metadata in the admin panel?

I’ve customized my comment form with a new field (city, but that doesn’t really matter). I’ve hooked and filtered and added my comment metadata with add_comment_metadata.

I want to show that metadata in the Comments admin panel (wp-admin/edit-comments.php). Google searches prove fruitless.

Is this possible? If it is, how does one go about it?

I’d like to show it in the author column, but I’ll settle for viewing the metadata in the edit view for each individual comment.

1 Answer
1

To show the content on individual edit pages you need to add custom meta boxes to the comment edit page. You’ll use the key comment for the $page argument of add_meta_box.

<?php
add_action( 'add_meta_boxes_comment', 'pmg_comment_tut_add_meta_box' );
function pmg_comment_tut_add_meta_box()
{
    add_meta_box( 'pmg-comment-title', __( 'Comment Title' ), 'pmg_comment_tut_meta_box_cb', 'comment', 'normal', 'high' );
}

function pmg_comment_tut_meta_box_cb( $comment )
{
    $title = get_comment_meta( $comment->comment_ID, 'pmg_comment_title', true );
    wp_nonce_field( 'pmg_comment_update', 'pmg_comment_update', false );
    ?>
    <p>
        <label for="pmg_comment_title"><?php _e( 'Comment Title' ); ?></label>;
        <input type="text" name="pmg_comment_title" value="<?php echo esc_attr( $title ); ?>" class="widefat" />
    </p>
    <?php
}

You’ll also probably want to be able to save changes from the admin. You can hook into edit_comment to do that.

<?php
add_action( 'edit_comment', 'pmg_comment_tut_edit_comment' );
function pmg_comment_tut_edit_comment( $comment_id )
{
    if( ! isset( $_POST['pmg_comment_update'] ) || ! wp_verify_nonce( $_POST['pmg_comment_update'], 'pmg_comment_update' ) )
        return;
    if( isset( $_POST['pmg_comment_title'] ) )
        update_comment_meta( $comment_id, 'pmg_comment_title', esc_attr( $_POST['pmg_comment_title'] ) );
}

I wrote a tutorial about this if you’re interesting in learning more — the code snippets above are from it.

To show data in the comment list table is similar to adding custom columns to post list tables.

To make sure we get the right screen, hook into load-edit-comments.php, grab the current screen and then hook into manage_{$screen->id}_columns to add another column to the comment list table.

<?php
add_action('load-edit-comments.php', 'wpse64973_load');
function wpse64973_load()
{
    $screen = get_current_screen();

    add_filter("manage_{$screen->id}_columns", 'wpse64973_add_columns');
}

The function you hook into manage_{$screen->id}_columns just needs to alter the associative array to include the column. It’s a $key => $label pair — make sure to remember the key, we’ll use it later. Going to stick with the comment title stuff above.

Finally, we need to hook into manage_comments_custom_column to echo out the comment title in the actual list table. This is pretty straight forward. I used a switch statement here because easily expandable to include more custom columns.

Any column $key you added earlier will be the first argument passed into the hooked function.

<?php
add_action('manage_comments_custom_column', 'wpse64973_column_cb', 10, 2);
function wpse64973_column_cb($col, $comment_id)
{
    // you could expand the switch to take care of other custom columns
    switch($col)
    {
        case 'title':
            if($t = get_comment_meta($comment_id, 'pmg_comment_title', true))
            {
                echo esc_html($t);
            }
            else
            {
                esc_html_e('No Title', 'wpse64973');
            }
            break;
    }
}

The code from the comment tut and the above stuff for custom list table columns is here as a plugin.

Leave a Comment