How to Show Useragent String for Comments in WP Admin Area?

You mean on the public facing page ?
I guess so.
Something like this in your functions file :

add_filter( 'comment_form_field_comment', 'my_comment_agent' );
function my_comment_agent( $comment_field ) {
    return $comment_field.'<input type="hidden" name="browser" value="$_SERVER['HTTP_USER_AGENT']" aria-req="true">'. ;
}

You could save the data if you needed to, something like :

add_action( 'comment_post', 'save_comment_browser' );
function save_comment_browser( $comment_id ) {
    add_comment_meta( $comment_id, 'browser', $_POST[ 'browser' ] );
}

Code all untested!

You want to add another column to the comment list table in the admin area, correct? There are filters and actions within WP_Comments_List_Table class that allow us to add custom columns. How this is done isn’t that well documented. The reference I rely upon is here.

Thanks guys for your replies and help. Yes, I was talking about adding Useragent column to Admin area.

@bcworkz
Thanks for the links. It seems the task is not so easy as I was expecting. I wonder why does WordPress not show Useragent column by default when the info is stored in database?

I hope in future, WordPress will add the required column to comment editing page.

Thanks again.

> I wonder why does WordPress not show Useragent column

Why would it ?

Adding filters in your functions file is pretty straightforward.

I think most WP users wouldn’t know (or care) how to use user agent information. WP designers do not want to overwhelm users with too much information, yet provide some mechanism for more sophisticated users to access the information they want.

Speaking of too much information, the article I linked to previously apparently overwhelmed you as well. There is a lot of information there, but what pertains to adding a new column is relatively simple. The hard part is extracting the parts you need from all the other information. You only need two filter/action callbacks. Use 'manage_edit-comments_columns' filter to add your column to the list and 'manage_edit-comments_custom_column' action to output cell content.

The example code in the article is for a custom post type list table but what you need is very close to the same except you’d be getting the current comment object instead of post meta in order to output the user agent.

@bcworkz
Thanks again. I’ll try to implement those instructions.



Source link