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!
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!
Share Improve this question edited Jan 13, 2021 at 7:37 klewis asked Jan 12, 2021 at 20:49 klewisklewis 8991 gold badge14 silver badges29 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 1Just 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. I've tested it out for myself in 2020, and concepts still work.
?><pre><?php var_dump( $fields['source'] ); ?></pre><?php
at the bottom of my functions.php file, but the output on my browser window says its null. – klewis Commented Jan 12, 2021 at 21:11