Skip to content

Instantly share code, notes, and snippets.

@ScottPhillips
Created July 29, 2012 08:48
Show Gist options
  • Select an option

  • Save ScottPhillips/3196798 to your computer and use it in GitHub Desktop.

Select an option

Save ScottPhillips/3196798 to your computer and use it in GitHub Desktop.
Modify Comment Fields in WordPress
<?php
//We can add a field as follows:
function add_comment_fields($fields) {
$fields['age'] = '<p class="comment-form-age"><label for="age">' . __( 'Age' ) . '</label>' .
'<input id="age" name="age" type="text" size="30" /></p>';
return $fields;
}
add_filter('comment_form_default_fields','add_comment_fields');
//Once we add the field depending on the theme we might want to style it. As I am using the Twenty Eleven theme I style it by adding #respond .comment-form-age label with the other labels style like #respond .comment-form-url label etc. as follows:
/*
#respond .comment-form-author label,
#respond .comment-form-email label,
#respond .comment-form-url label,
#respond .comment-form-age label,
#respond .comment-form-comment label {
background: #eee;
-webkit-box-shadow: 1px 2px 2px rgba(204,204,204,0.8);
-moz-box-shadow: 1px 2px 2px rgba(204,204,204,0.8);
box-shadow: 1px 2px 2px rgba(204,204,204,0.8);
color: #555;
display: inline-block;
font-size: 13px;
left: 4px;
min-width: 60px;
padding: 4px 10px;
position: relative;
top: 40px;
z-index: 1;
}
*/
function add_comment_meta_values($comment_id) {
if(isset($_POST['age'])) {
$age = wp_filter_nohtml_kses($_POST['age']);
add_comment_meta($comment_id, 'age', $age, false);
}
}
add_action ('comment_post', 'add_comment_meta_values', 1);
echo 'Comment authors age: ' . get_comment_meta( $comment->comment_ID, 'age', true );
function add_comment_fields($fields) {
if( is_singular( 'books' ) ) {
$fields['age'] = '<p class="comment-form-age"><label for="age">' . __( 'Age' ) . '</label>' .
'<input id="age" name="age" type="text" size="30" /></p>';
}
return $fields;
}
add_filter('comment_form_default_fields','add_comment_fields');
//In the Twenty Eleven theme in comments.php you will see a line:
wp_list_comments( array( 'callback' => 'twentyeleven_comment' ) );
//This we will change to:
wp_list_comments( array( 'callback' => 'my_comments_callback' ) );
//So the function my_comments_callback will be called for each post.
function my_comments_callback( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<article id="comment-<?php comment_ID(); ?>" class="comment">
<div class="comment-content"><?php comment_text(); ?></div>
<p><?php echo "Comment authors age: ".get_comment_meta( $comment->comment_ID, 'age', true ); ?></p>
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply <span>&darr;</span>', 'twentyeleven' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div>
</article>
</li>
<?php
}
//We also modify the background color as follows:
/*
.commentlist > li.comment {
background: #99ccff;
border: 3px solid #ddd;
-moz-border-radius: 3px;
border-radius: 3px;
margin: 0 0 1.625em;
padding: 1.625em;
position: relative;
}
*/
function remove_comment_fields($fields) {
unset($fields['url']);
return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');
@securemailpro
Copy link

This was a really engaging post. I appreciate the clarity and depth of information. Looking forward to more of your writing!
8th Pay Commission pay calculator

@securemailpro
Copy link

Great insights and easy-to-follow structure. You’ve made it effortless to grasp even for beginners. Fantastic work!
Gamepad Test

@gen-tools
Copy link

Really enjoyed reading this! The info feels super relevant and easy to follow — great job pedrovazpaulo business consulting

@gen-tools
Copy link

I love the way you broke everything down step by step. qullnowisfap

@gen-tools
Copy link

This article is so well-written! I love how you broke down the complex ideas into simple terms. I’ll definitely be coming back for more posts like this. Keep up the amazing work! the unseen project website

@gen-tools
Copy link

This was a pleasure to read! You clearly have expertise in this area, and it shows through the way you explained each detail so precisely. pedrovazpaulo marketing consulting

@rossd4828-hubed
Copy link

Qullnowisfap in cleansing provides a natural solution to impurities, enhancing your skin’s beauty and overall health. what is qullnowisfap in cleansing

@gen-tools
Copy link

Vidizzy — Plateforme gratuite de chat vidéo aléatoire. Parlez instantanément avec des inconnus, sans inscription ni téléchargement, directement depuis votre navigateur. Vidizzi

@gen-tools
Copy link

Vidizzy — Kostenloser Zufalls-Videochat. Mit nur einem Klick neue Leute weltweit treffen, ganz ohne Anmeldung oder App-Download. Vidizzi

@gen-tools
Copy link

Gydoo — Free gay and bi video chat site. Meet new people instantly in a safe, anonymous environment — no signup required. gay video chat for adults

@securemailpro
Copy link

Wow, I didn’t even know a Doraemon X APK existed! Downloaded it and it’s actually super smooth. The interface feels really clean, and I love all the mini games included. Anyone else tried it on Android 12?

@securemailpro
Copy link

Nice site! I love that you highlight “trusted local experts” and use quality parts. As someone who hates dealing with appliance issues, it’s good to know there’s a reliable option in Las Cruces. https://appliancerepairlascruces.com/

@securemailpro
Copy link

I always check the Whataburger nutrition info first, so I can pretend I'm ‘eating healthy’ even tho I still order the same spicy stuff every time whataburger nutrition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment