3


I have few custom fields in comment form. How can I add these fields to comments metabox? I divide my question on 3 parts:

1) How to remove url field and add custom comment fields to comment quick edit form?

comments metabox

2) How to add custom fields to comment add form?

comment add form

3) Hot to add custom meta information to comment metabox here?

comment list metabox

  • Can you post code of what you tried to achieve this? And I suppose you may need to ask it as three different questions, it's not as simple as that.These additions need to be done via JavaScript as the comments are fetched via AJAX response using WP_Post_Comments_List_Table and fields are copied from predefined form table via wp_comment_reply(). – bravokeyl Jul 17 '16 at 5:26
  • I tried to find a hook to modify this metabox, but without success. I posted this questions in one place, because it's a one metabox. – anton Jul 17 '16 at 10:02
0

Unfortunately the hooks:

manage_{$this->screen->id}_columns
manage_{$this->screen->id}_sortable_columns
manage_comments_custom_column

are not available for the post-comments list table, constructed within the wp_ajax_get-comments call. That table consists only of two columns: author and comment. The data for author, avatar, email, url and IP is displayed in the first column.

We can on the other hand use a hack like:

add_filter( 'get_comment_author_IP', function( $comment_author_IP, $comment_ID, $comment )
{
    if( doing_action( 'wp_ajax_get-comments' ) )
        echo 'Some Custom Text <br/>';

    return $comment_author_IP;
}, 10, 3 );

to display some data, in the first column, for each row. We could e.g. use a get_user_meta() call here.

Regarding how we can add custom input fields to the quick-edit comment form, we can also use a hack like this one:

add_filter( 'the_editor', function( $html )
{   
    if( did_action( 'load-post.php' ) && false !== strpos( $html, 'id="replycontent"' ) )
        $html .= sprintf( 
            '<br/> %s <input type="text" name="newcomment_myphone" value="%s">',
            esc_html__( 'Phone', 'wpse' )
        );

    return $html;
} );

where we target the replycontent editor it on the post.php screen. This might need further restriction. The field displays below the comment content editor:

custom field

It should then be possible to hook into the edit_comment action to store it into the user meta.

This should also work for the Add comment form but you might need javascript to insert the current value to the custom field. I'm sure it's also possible to inject the custom fields, to the comment form, with javascript instead of the approach listed above.

You could try to hide the URL field with CSS:

#author-url, label[for="author-url"] {
    display:none;
}

Hopefully you can adjust this further.

  • Sorry for late reply. With your help I solved my problem. Thanks!) – anton Apr 24 '17 at 15:49
  • no problem, glad to hear it helped @anton – birgire Apr 24 '17 at 15:50
  • By the way, how we can check if comment updated? When wordpress loads comments, doing_action( 'wp_ajax_get-comments' ) check works well, but there should be another check something like when comment updated to refresh values. – anton Apr 24 '17 at 16:34
  • I'm not sure exactly what you mean (javascript or php check), but maybe it would be better served as a new question instead? - also because it's been a while since I looked at this problem so others might be more fresh on the topic than I am now ;-) @anton – birgire Apr 24 '17 at 16:43
  • take a look @birgire , maybe you know the answer - wordpress.stackexchange.com/questions/264764/… – anton Apr 25 '17 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.