Filter for admin (back end) ‘reply to’ comment

Is there a filter for the adding of fields to the comment box form
that is on the admin/comments page?

If you meant the inline form for editing or replying to a comment at wp-admin/edit-comments.php, then,

  • The form is outputted using wp_comment_reply() which has a hook with the same name, i.e. wp_comment_reply, and you could use that to return your own comment reply form HTML.

  • However, the inline editing is a JavaScript feature, hence I would simply use JS to add custom fields to that form.

Working Example

So here’s a sample script for adding a field named hidden_field (labeled Hidden Field) to that form:

jQuery( function ( $ ) {
    // Append the field at the bottom of the inline form, above the submit
    // button. Just customize the HTML, but ensure the selector is correct.
    // ( i.e. I used [name="hidden_field"], so change it based on your HTML. )
    $( '#replysubmit' ).before(
        '<p style="padding: 3px 0 2px 5px; clear: both">' +
            '<label>Hidden Field:</label> ' +
            '<input name="hidden_field" />' +
        '</p>'
    );

    // Note: (window.)commentReply is defined by WordPress.
    $( '#the-comment-list' ).on( 'click', '.comment-inline', function() {
        var $field = $( '#replyrow input[name="hidden_field"]' );

        // If the Quick Edit button is clicked, set the field value to the
        // current database value.
        if ( 'edit-comment' === commentReply.act ) {
            $field.val( $( '#hidden_field-' + commentReply.cid ).val() );
        } else {
        // If the Reply button is clicked, then we empty the field.
            $field.val( '' );
        }
    } );

    // Submit the form when the Enter key is pressed.
    $( '#replyrow input[name="hidden_field"]' ).on( 'keypress', function( e ) {
        if ( e.which == 13 ) {
            commentReply.send();
            e.preventDefault();
            return false;
        }
    } );
} );
  1. Save it to an external JS file and load the script on the comments page, e.g. via the admin_enqueue_scripts hook, like so: ( make sure admin-comments which loads wp-admin/js/edit-comments.js, is in the dependencies list )

    add_action( 'admin_enqueue_scripts', 'my_admin_enqueue_scripts' );
    function my_admin_enqueue_scripts() {
        if ( 'edit-comments' === get_current_screen()->id ) {
            wp_enqueue_script( 'my-script', '/path/to/the/script.js',
                array( 'admin-comments' ) );
        }
    }
    
  2. To save the field, e.g. as a comment metadata, you can use the comment_post and (the one you already using) edit_comment hooks. For example:

    add_action( 'edit_comment', 'my_save_comment_hidden_field' ); // for the Quick Edit button
    add_action( 'comment_post', 'my_save_comment_hidden_field' ); // for the Reply button
    function my_save_comment_hidden_field( $comment_ID ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
        }
    
        if ( ! isset( $_POST['hidden_field'] ) ||
            ! current_user_can( 'edit_comment', $comment_ID )
        ) {
            return;
        }
    
        $value = sanitize_text_field( $_POST['hidden_field'] );
    
        update_comment_meta( $comment_ID, 'hidden_field', $value );
    }
    
  3. Make sure to add a hidden input which stores the field value that’s currently in the database. But it does not have to be an <input />.. you could just use other element; what’s important is, store the value somewhere so that JS can get the value and update the field value in the inline form when editing a comment, i.e. after clicking on “Quick Edit”.

    So for example, I added the hidden input via the comment_text hook:

    add_filter( 'comment_text', 'my_comment_text', 10, 2 );
    function my_comment_text( $comment_text, $comment ) {
        $value = $comment ? get_comment_meta( $comment->comment_ID, 'hidden_field', true ) : '';
    
        $input = sprintf( '<input type="hidden" id="hidden_field-%d" value="%s" />',
            $comment->comment_ID, esc_attr( $value ) );
    
        return $comment_text . $input;
    }
    

tech