Looking for the filter hook for the admin/comments/reply to process (where you use 'reply to' in the comments administration screen). This will be used in a custom plugin.
(The plugin adds a hidden field to a front-end entered comment to reduce bot-entered comments. The extra field is inserted properly on the front end, but not in the admin comment list page.)
I need to
- add a hidden field to the comment drop-down on the comments admin list (the comment field that shows when you click the 'reply' link for a comment).
- add some pre-processing to the comment drop-down when it is submitted and before WP processes the comment.
Not sure where to start on this, although I think it is in .php/ .
Am I on the right track? Is there a filter for the adding of fields to the comment box form that is on the admin/comments page?
(Note: had posted this question in StackExchange main area by mistake. And got a down-vote and close vote without explanation for my efforts. So duplicating the question here - where I meant to put it.)
Added
The hidden field is added to the comment form on the front end with the 'add_meta_boxes_comment' action hook, which uses the 'add_meta_box' function to specify the field and the callback function for that inserted/hidden field.
The hidden/added field is processed via the 'edit_comment' hook. If the hidden field is not there, then the comment is not saved.
This works fine in the front-end, but the hidden/added field is not added to the comment form in the back-end's "Reply" on the comment list page.
Looking for the filter hook for the admin/comments/reply to process (where you use 'reply to' in the comments administration screen). This will be used in a custom plugin.
(The plugin adds a hidden field to a front-end entered comment to reduce bot-entered comments. The extra field is inserted properly on the front end, but not in the admin comment list page.)
I need to
- add a hidden field to the comment drop-down on the comments admin list (the comment field that shows when you click the 'reply' link for a comment).
- add some pre-processing to the comment drop-down when it is submitted and before WP processes the comment.
Not sure where to start on this, although I think it is in https://developer.wordpress/reference/files/wp-admin/includes/class-wp-comments-list-table.php/ .
Am I on the right track? Is there a filter for the adding of fields to the comment box form that is on the admin/comments page?
(Note: had posted this question in StackExchange main area by mistake. And got a down-vote and close vote without explanation for my efforts. So duplicating the question here - where I meant to put it.)
Added
The hidden field is added to the comment form on the front end with the 'add_meta_boxes_comment' action hook, which uses the 'add_meta_box' function to specify the field and the callback function for that inserted/hidden field.
The hidden/added field is processed via the 'edit_comment' hook. If the hidden field is not there, then the comment is not saved.
This works fine in the front-end, but the hidden/added field is not added to the comment form in the back-end's "Reply" on the comment list page.
Share Improve this question edited Jan 20, 2022 at 22:45 Rick Hellewell asked Jan 19, 2022 at 19:35 Rick HellewellRick Hellewell 7,1312 gold badges22 silver badges41 bronze badges 4- 1. "add a hidden field" - you mean, a hidden field with a value that remains unchanged on the same page, regardless which comment being edited or replied to? 2. "add some pre-processing" - what kind of pre-processing? How did you do that on the front-end side (what hook you used)? – Sally CJ Commented Jan 20, 2022 at 7:33
- 1 Added additional info to the question per the request of the above comment. – Rick Hellewell Commented Jan 20, 2022 at 22:46
- So I wondered if my answer answered your question, either in full or just partially? Could you please let me know? – Sally CJ Commented Jan 24, 2022 at 9:06
- 1 Your answer was helpful, but I decided to do it differently to reduce complexity. My solution (below) worked for my application. My solution might help others, but so will your answer. – Rick Hellewell Commented Jan 24, 2022 at 19:06
2 Answers
Reset to default 2Is 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', 'ment-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;
}
} );
} );
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 sureadmin-comments
which loadswp-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' ) ); } }
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 ); }
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; }
Although @SallyCJ's answer might work, and is probably good information, I went in a different direction that worked for my application.
My application needs a hidden field in the comment form. Although you can easily add the hidden field to the front-end comment form, the back-end doesn't use that process. But my application needs the POST of that hidden value for a verification process.
Since the hidden field isn't easily insertable in the back-end drop-down comment form (that you get when you hit the reply button), I reasoned that only an authorized admin can get to the admin/comment list, so my application doesn't need to check if it is a bot accessing the comment form. (My plugin senses bot comment submissions.) If you are logged in as an admin, then you are not a bot.
So, in the section that checks for the hidden field, I added this to add the GUID value to 'the_hidden_field' that is shown on the front-end comment form:
if (current_user_can( 'moderate_comments' ) ) {
// forces the guid on admin/comment replies without needing
// to add a hidden field to the dropdown form.
$_POST['the_hidden_field'] = wp_generate_uuid4();
return $commentdata;
}
The 'if' statement will be true for the admin/editor/author roles only, so the hidden field is sort of added to the comment POST.
Note that others have said that you can use is_admin() to see if you are in an admin page, but that returns false in some instances - like if you are in the admin/list comments page, and you hit the 'reply' button for a comment. An is_admin() check there returns false. So that didn't work.
Adding the code above resolved my issue. Might not work for all - you may need the code suggested by @SallyCJ . But it worked for me.