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'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.

share|improve this question
    
Where exactly in the admin UI? Please edit your question to clarify. Thanks. –  kaiser Sep 12 '12 at 17:14

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Excellent answer! Comprehensive and clear. Rather than a whole new column, could I append this to the Author column, without wrecking the rest of the data in the Author column? My guess is probably, but how? –  ageektrapped Sep 13 '12 at 18:01
1  
No, there's not really an easy way to do that. You should take a look at the WP_List_Table class, a subclass of which is used to display the comments. Basically it dispatches each column to a method called column_{$key}, if that doesn't exist, it falls to back to column_default. If you look at the callback for the author column there are no hooks to use. –  chrisguitarguy Sep 13 '12 at 18:18
    
Hi there, great answer. Would you fix the URL to pmg.co? It doesn't lead to the article that it should (just a blank page). –  halfer Jul 6 '13 at 18:04

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.