How to display custom comment fields onto any page

In the latest version of WordPress, if I added the following code to include a custom field to my functions.php file…

function add_comment_fields($fields) {
    $fields['source'] = '<p class="comment-form-source ast-col-xs-12 ast-col-sm-12 ast-col-md-4 ast-col-lg-4"><label for="source" class="screen-reader-text">' . __( 'Related Publishing Source' ) . '</label>' .
        '<input id="source" name="source" type="text" value="" placeholder="Publishing Source" size="30" aria-required="true" /></p>';
    return $fields;
}
add_filter('comment_form_default_fields','add_comment_fields');

…how do I “display the results” of this custom field onto the comments page for each comment posted?

I was looking at the get_comments function, with an example like so….

<?php
$args = array(
    'user_id' => 1, // use user_id
);
$comments = get_comments( $args );
 
foreach ( $comments as $comment ) :
    echo $comment->comment_author . '<br />' . $comment->comment_content;
endforeach;

But that relates to the OOTB comments provided my wordpress. How can we display our new custom fields in this same fashion?

I found this article from 2011 on what I am trying to achieve. WordPress has changed dramatically sense then. Is there a 2020 version of that article for WordPress 5+ …?

Many thanks!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Just in case others are stumped with trying to learn how to reveal submitted comments onto a comments page (or any page for this matter), a good starting point to investigate is using the WordPress function wp_list_comments()

There are examples provided on that resource page to get things going in a healthy direction.

I would also include the following debug tactic as a help along the way…

?><pre><?php var_dump( $variable_to_test ); ?></pre><?php

…as mentioned by Q Studio. Thanks for that!

Update

Another good resource to help extend the OOTB comment fields with more fields by WPengineer.com. I’ve tested it out for myself in 2020, and concepts still work.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x