Join over 55,891 Subscribers Today! FREE UPDATES!
Get The Only Freelancer crash course you will ever need to read!
With the release of WordPress 3.0, a new function is given to us to use. comment_form() will display the comment form on any given page with ease. This will be good for both theme developers and plugin developers alike. This new function opens up new doors and allows us to modify things that before were much more complicated.
In this article I will explain how to use the new comment_form() function within WordPress 3.0 to give better usability within our comments function inside of WordPress.
To use this function you can open up your comments.php file within your theme and view the code that should look similar to the following:
<?php if ('open' == $post->comment_status) : ?>
<div id="respond">
<h3><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>
<div class="cancel-comment-reply">
<small><?php cancel_comment_reply_link(); ?></small>
</div>
<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
You must be <a href="<?<span class="><?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=">logged in</a> to post a comment.
<pre><?php else : ?></pre>
<form action="<?<span class="><?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform"></form>
<?php if ( $user_ID ) : ?>
Logged in as <a href="<?<span class="><?php echo get_option('siteurl'); ?>/wp-admin/profile.php"></a>. <a title="Log out of this account" href="<?php echo wp_logout_url(get_permalink()); ?>">Log out »</a>
<?php else : ?>
<input id="author" name="author" type="text" value="<?<span class=" /><?php echo $comment_author; ?>" size="22" tabindex="1" />
<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label>
<input id="email" name="email" type="text" value="<?<span class=" /><? php echo $comment_author_email; ?>" size="22" tabindex="2" />
<label for="email"><small>Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label>
<input name="<span class=" type="text" />url" id="url" value="" size="22" tabindex="3" />
<label for="<span class=">url"><small>Website</small></label>
<?php endif; ?>
<!--<small><strong>XHTML:</strong> You can use these tags: <code></code></small>
-->
<textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4">
<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
<?php comment_id_fields(); ?>
</p>
<?php do_action('comment_form', $post->ID); ?>
</form>
<?php endif; // If registration required and not logged in ?>
</div>
<?php endif; // if you delete this the sky will fall on your head ?>
</div>
You can delete the above code in comments.php file and replace it with the following:
<?php comment_form(); ?>
The WordPress 3.0 function – comment_form() has 2 parameters that can be optionally modified to your liking: Here is the example arguments that can be used:
<?php comment_form($args, $post_id); ?>
Now to explain what each of these parameters do so we know how to use them:
$args: This contains our options for our strings and fields within the form and etc.
$post_id: Post ID is used to generate the form, if null it will use the current post.
The $arg parameter uses the following values by default:
<?php $defaults = array( 'fields' => apply_filters( 'comment_form_default_fields', array(
'author' => '<p class="comment-form-author">' .
'<label for="author">' . __( 'Name' ) . '</label> ' .
( $req ? '<span class="required">*</span>' : '' ) .
'<input id="author" name="author" type="text" value="' .
esc_attr( $commenter['comment_author'] ) . '" size="30" tabindex="1"' . $aria_req . ' />' .
'</p><!-- #form-section-author .form-section -->',
'email' => '<p class="comment-form-email">' .
'<label for="email">' . __( 'Email' ) . '</label> ' .
( $req ? '<span class="required">*</span>' : '' ) .
'<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30" tabindex="2"' . $aria_req . ' />' .
'</p><!-- #form-section-email .form-section -->',
'url' => '
<p class="comment-form-url">' .</p>
'<label for="url">' . __( 'Website' ) . '</label>' .
'<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" tabindex="3" />' .
'
<!-- #<span class="hiddenSpellError" pre="">form-section-url</span> .form-section -->' ) ),
'comment_field' => '<p class="comment-form-comment">' .
'<label for="comment">' . __( 'Comment' ) . '</label>' .
'<textarea id="comment" name="comment" cols="45" rows="8" tabindex="4" aria-required="true"></textarea>' .
'</p><!-- #form-section-comment .form-section -->',
'must_log_in' => '
<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>
',
'logged_in_as' => '
<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%s">%s</a>. <a title="Log out of this account" href="%s">Log out?</a></p>
' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ),
'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email is <em>never</em> published nor shared.' ) . ( $req ? __( ' Required fields are marked <span class="required">*</span>' ) : '' ) . '</p>',
'comment_notes_after' => '<dl class="form-allowed-tags"><dt>' . __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes:' ) . '</dt> <dd><code>' . allowed_tags() . '</code></dd>',
'id_form' => 'commentform',
'id_submit' => 'submit',
'title_reply' => __( 'Leave a Reply' ),
'title_reply_to' => __( 'Leave a Reply to %s' ),
'cancel_reply_link' => __( 'Cancel reply' ),
'label_submit' => __( 'Post Comment' ),
); ?>
The parameters of $arg are self-defined by names. Also remember that you can leave $arg as null if you wish and it will use the default.
You can also customize $arg using the comment_form_default_fields filter.
Example: If you wanted to hide the URL field on your form, you can simply insert the following code to your functions.php file:
add_filter('comment_form_default_fields', 'mytheme_remove_url');
function mytheme_remove_url($arg) {
$arg['url'] = '';
return $arg;
}
All fields are also each passed through a filter of the form comment_form_field_$name
This gives us more options to use for each way of changing our output.
$name is the value used in the array of fields. So we can also hide it using the following:
add_filter('comment_form_default_fields', 'mytheme_remove_url');
function mytheme_remove_url($arg) {
$arg['url'] = '';
return $arg;
}
Using the comment_form() function is an excellent way to keep the code within your template clean. It gives you more ways to customize the comments form using sets of filters.
For users of WordPress 3.0 it is suggested you use this new code to replace the old style comment form code.
Using more hooks to customize our comments_form. Here are some more options and examples to show more on how this can be used.
The first hook is comment_form_default_fields. This lets us modify the three main fields: author, email, and website. It’s a filter, so we can change things as they pass through it. The fields are stored in an array which contains the html that is output. So it looks sorta like this:
array( 'author' => '<p class="comment-form-author">...', 'email' => '<p class="comment-form-email">...', 'url' => '<p class="comment-form-url">...' );
This simple method shows the generic way the code can display, but using other modifications you can change more fields like this:
function my_fields($fields) {
$fields['new'] = '<p>a new input field is here</p>';
return $fields;
}
add_filter('comment_form_default_fields','my_fields');
This allows us to add a new input field, or to modify the existing fields.
Fields are not the only items that can be changed. There is also a comment_form_defaults filter as well. It gets the surrounding text of the comments form.
$defaults = array( 'fields' => apply_filters( 'comment_form_default_fields', $fields ), 'comment_field' => '<p class="comment-form-comment">...', 'must_log_in' => '<p class="must-log-in">...', 'logged_in_as' => '<p class="logged-in-as">...', 'comment_notes_before' => '<p class="comment-notes">...', 'comment_notes_after' => '<dl class="form-allowed-tags">...', 'id_form' => 'commentform', 'id_submit' => 'submit', 'title_reply' => __( 'Leave a Reply' ), 'title_reply_to' => __( 'Leave a Reply to %s' ), 'cancel_reply_link' => __( 'Cancel reply' ), 'label_submit' => __( 'Post Comment' ), );
Everything that is displayed as a section of the comment form area are defined in this one place, so they can be modded to your liking.
Remember, that unlike the fields, when you add new parts here it won’t change anything. The fields get pushed through for display, so these
are just settings that get used randomly.
Filters are not the only way to change this. The comment_form function can accept an array of arguments as different parameters, and each of those
will modify the form as well. If you wanted to make a simple modification to alter the wording of “leave a reply” – Then you could change the following:
<?php comment_form(array('title_reply'=>'Leave a Reply, And Voice Yourself')); ?>
This allows us to have a more simple way of making changes without all the hassle of filters, but remember that the filters can also be useful and more powerful for other operations.
While the comment form is generated there are also some action hooks being called. If you choose to put something into the form itself while it is being generated you can do so by using the following:
• comment_form_before
• comment_form_must_log_in_after
• comment_form_top
• comment_form_logged_in_after
• comment_notes_before
• comment_form_before_fields
• comment_form_field_{$name} (a filter on each and every field, where {$name} is the key name of the field in the array)
• comment_form_after_fields
• comment_form_field_comment (a filter on the “comment_field” default setting, which contains the textarea for the comment)
• comment_form (action hook after the textarea, mainly for backward compatibility)
• comment_form_after
• comment_form_comments_closed
Don’t forget your styling! Each part of the comment form contain classes and id’s that can be styled using CSS. You can check out the HTML output and see all of the entries to style to your liking. The output is also semantic and contains label tags.
I myself have been using this new function in creating themes for WordPress 3.0 and have found it to be really useful. If used correctly you can accomplish many different results that can better help your coding of a comments form for yourself or a client. Play around with the code and have fun – Feel free to ask any questions. Enjoy!
Get The Only Freelancer crash course you will ever need to read!
My name is Kevin. I am a 30 year old freelance web designer. I have been working with HTML, CSS, and PHP for 6+ years - And creating websites using Wordpress for 3 years. I enjoy creating websites and also doing some graphic design using Photoshop and Illustrator. I hope everyone enjoys reading my articles and I look forward to your questions, comments, and feedback.
Tuesday, September 6th, 2011 22:18
Very interesting points you have remarked, thank you for putting up.
Tuesday, September 6th, 2011 16:41
Really revealing thank you, I do think your readers might just want far more content along these lines maintain the good content.
Sunday, September 4th, 2011 20:59
I intended to compose you that very little word in order to say thanks a lot yet again for these breathtaking tips you have contributed in this article. It is quite pretty generous of you giving without restraint all that many individuals could possibly have distributed for an ebook to generate some cash for themselves, specifically considering that you might have tried it in case you desired. The tactics in addition worked as a fantastic way to be aware that other individuals have the identical desire much like mine to realize way more on the topic of this problem. I think there are millions of more pleasurable times ahead for individuals who look into your blog post.
Sunday, September 4th, 2011 19:42
Particularly informative many thanks, I believe your current readers might want far more well written articles like this maintain the excellent work.
Sunday, September 4th, 2011 05:28
Awfully illuminating thank you, I presume your trusty subscribers could very well want a lot more writing like this continue the good content.
Saturday, September 3rd, 2011 03:53
How would I change the comments form to be shown to “editors” only? This even logged in, subscribers would not be shown the comments form throughout my site. Could you provide the statement(s) I have to add and point where on the code the statements should be placed.
Thanks a bunch
Friday, September 2nd, 2011 14:50
Hey, you used to write wonderful, but the last few posts have been kinda boring… I miss your tremendous writings. Past several posts are just a bit out of track! come on!”To dare is to lose one’s footing momentarily. To not dare is to lose oneself.” by Sren Aaby Kierkegaard.
Thursday, September 1st, 2011 08:02
I was browsing the web and came across your website. I absolutely enjoyed reading your posts and looking forward to more great content.
Monday, August 29th, 2011 20:27
That is why we love WordPress…easy to learn, easy to install, easy to configure, great presentation layer…
Tuesday, August 23rd, 2011 18:26
I wanted to start my own blog and I was looking for your suggestions for which sites you think are the best to start one with. . I want to be able to post pictures, text, and music (maybe).. I only want to use a free blog host..
Tuesday, August 23rd, 2011 18:19
THank you the code helped me alot with some comments options.
Monday, August 22nd, 2011 10:04
I wish to show my affection for your kindness giving support to people that should have help on this content. Your very own dedication to passing the message throughout has been especially advantageous and has usually made ladies just like me to reach their targets. Your personal interesting hints and tips can mean a lot a person like me and extremely more to my colleagues. Best wishes; from all of us.
Monday, August 22nd, 2011 04:18
Hey, I was thinking, do you ever welcome guest content? If that’s the case, can you please let me know what would I have to do to be able to write an article for this blog?
Saturday, August 20th, 2011 00:52
Thanks for a very interesting web site. What else could I get that kind of information written in such an ideal means? I’ve a mission that I am just now working on, and I have been at the look out for such info.
Monday, August 15th, 2011 12:23
This is just what I’ve been looking for. Thanks for the scripts, it really helped me.
Monday, August 15th, 2011 10:06
Very nice post. I just stumbled upon your weblog and wished to say that I have really enjoyed browsing your blog posts. After all I’ll be subscribing to your rss feed and I hope you write again soon!
Sunday, August 14th, 2011 22:31
i am working on alot of this now, and want to know what else we can do..question, does all o this require php knowledge?
Sunday, August 14th, 2011 08:35
I love your blog and just wanted to tell you how much I appreciate you posting!
Saturday, August 13th, 2011 16:06
I’m intrigued by the information you have written in this article. I happened upon your article and I’m glad I did. Your points are valid, well-represented and the article is easy to read and understand.
Saturday, August 13th, 2011 15:14
Been a long time reader of your blog, and just wanted to say keep up the good work!
Saturday, August 13th, 2011 09:32
Hi, thanks for this. I found your website from bing. If anyone requires any help which is website related, just contact me via my link.
Friday, August 12th, 2011 17:29
What an inspiring blog you wrote! I totally like the useful info shared in the blog.
Wednesday, August 10th, 2011 23:34
Frankly, is it realizable to publish 14,000 woodworking plans? It seems that John is the living substantiation! These woodworking plans are appropriate for multifarious projects, starting from coffee eatables plans and shelf plans, through playhouse plans to scullery or harshly branch projects. Further, there is more than story rendition payment all the plans mentioned. Yes, you can rearrange the complete about with woodworking4home.com combination of plans.
Wednesday, August 10th, 2011 17:08
From sliding automatic doors, safety doorsor full shop facias with aluminium systems, RTJ Solutions are the place to be for any form of door
Wednesday, August 10th, 2011 09:12
Thanks for the marvelous posting! I genuinely enjoyed reading it; you can be a great author. I will ensure that I bookmark your blog and will eventually come back from now on. I want to encourage continue your great job, have a nice holiday weekend!
If not, then it's time to learn how to:
You can trust 1stWebDesigner to help you become a better web designer!
- Jacob Cass | Just Creative
Just enter your name and email below and click Get Updates!
Ashish
Thursday, December 29th, 2011 17:56
Thanks For This Awesome Info! .
Marcia
Monday, January 9th, 2012 09:38
Good article,I like it very much!
Toko
Saturday, January 7th, 2012 18:27
Thank you very much.
Your information is very clear. In one hour, I manage to customize the whole aspects of my comment section.
James
Monday, January 2nd, 2012 07:28
I like the site it is nice thanks .
Shaun Kowalewski
Sunday, December 4th, 2011 09:14
Thanks for the HTML code. This is great, clean code for php. I can certainly use this code for the professional wordpress comment form. Thank you. Things just get better!
Dogan Kutbay
Thursday, December 1st, 2011 20:53
I copied your comments.php code, but its not working, there are some syntax problems.
But thanks anyway.
Pete
Wednesday, December 14th, 2011 11:08
There are several syntax error in the code unfortunately… Seems like the author simply copy / pasted it without paying any attentiont to testing it.
sam yulee
Tuesday, November 15th, 2011 11:41
I’m still attaining understanding of your stuff, but I’m making my in place too. I absolutely enjoy reading through through all that’s released inside your blog.Continue the great work. I loved it!
Ian
Thursday, November 10th, 2011 20:49
Just use your imagination. Stories or characters of movies that you watched and liked that you want to be made in a single story or movie.
Ian
Thursday, November 10th, 2011 20:48
We want the movie to be
~Really Scary (Jump out of your skin bout to cry scary)
~PG 13 (has to be PG13)
~Something that isn’t boring
~Possibly old (black and white, doesn’t have to be though)
Please find as many movies as you can. If you are able to find a website that include reviews about the movie too, that would be awesome, but you don’t have to! We don’t plan on going to the movies we just want to like rent a movie and play it at home!
Thanks in advance!
Dewayne Auber
Tuesday, November 15th, 2011 17:00
There are some attention-grabbing deadlines in this article but I don?t know if I see all of them heart to heart. There’s some validity however I’ll take hold opinion till I look into it further. Good article , thanks and we wish more! Added to FeedBurner as well
Hoxxy
Wednesday, November 16th, 2011 11:22
Thanks for the great info :)
Ulian
Wednesday, November 30th, 2011 13:17
I want to know further.plz give me your contact address
Derek
Monday, November 21st, 2011 22:06
thanks, that helped a lot. However, its still not clear how we can change the label of the textarea section. Its not in the filter as you posted it, and havent been able to find it somewhere else so far.
Stan
Monday, November 21st, 2011 20:39
I recently started a blog, and I was just wondering how people have promoted their blogs online to get more followers so you aren’t just typing to nothing out in the internet?
Oh, and for my blog, I don’t really want my friends to be my followers, unless they find it on their own. My blog is about something that most of my friend’s can’t really help me with. (Photography is the main subject)
Rishav
Monday, January 9th, 2012 14:35
Hi, I am blogger but have hesitation using the wordpress but it seems to be simple and easy to get it done.
John
Sunday, January 15th, 2012 07:39
Most poeple would not like these messy links to put in the wordpress, But if you can do it, this is a great. You have a great knowledge in this area. Thanks for the share.
Ogen laseren
Monday, March 12th, 2012 14:46
Thanks alot! My comment template has been redesigned!
Muddasser
Saturday, March 10th, 2012 16:11
I had some issues regarding this problem in the hooks. you made it really simple and i solved them. thanks.
ahmad tarik
Thursday, March 1st, 2012 03:04
Thank you for this very important information, continued progress.
Damian Smith
Wednesday, March 28th, 2012 03:57
Brilliant article thanks! Seems to work perfectly well with the latest versions of wordpress as well (3.3)
Made the code a lot cleaner as mentioned and seems to have made life easier when trying to implement the comments form into side areas ect. (Had a nightmare with previous use of the normal code)
Thanks for sharing :)
Milan
Saturday, March 31st, 2012 06:14
I drop a comment each time I like a article on a website or if I have something to valuable to contribute to the conversation. It’s a result of the fire communicated in the post I browsed. And after reading this article I was actually moved enough to drop a comment here
Lisa G.
Tuesday, April 24th, 2012 11:15
I was afraid to dive into php, but gave it a shot after reading this. Never really love WordPress’ comment system. Thanks for the tips to get me started.
Foong Cheng Leong
Tuesday, April 10th, 2012 05:17
I totally agree with what you are saying eventhough this article is not suit to new WP :)
Amitabha Roy
Saturday, April 7th, 2012 16:21
This post is really helpful. I am able to override ‘author’, ‘email’ and ‘url’ default settings, but I’m not able to override ‘comment_field’, it is getting duplicated, i.e. the default textarea is also appearing with my custom comment textarea. Any help would be appreciated.
Vendy
Tuesday, February 28th, 2012 11:39
Thanks for this clear explenation, but does someone know how to make a theme with the comment_form() also compatible with WordPress <3.0 versions?